File: mainloop.c

package info (click to toggle)
mol 0.9.61-6
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,140 kB
  • ctags: 8,491
  • sloc: ansic: 50,560; asm: 2,826; sh: 458; makefile: 373; perl: 165; lex: 135; yacc: 131
file content (303 lines) | stat: -rw-r--r-- 7,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* 
 *   Creation Date: <2001/01/27 14:38:03 samuel>
 *   Time-stamp: <2001/08/12 18:00:06 samuel>
 *   
 *	<rvec.c>
 *	
 *	
 *   
 *   Copyright (C) 2000, 2001 Samuel Rydh (samuel@ibrium.se)
 *   
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation
 *   
 */

#include "mol_config.h"

#include "mol_assert.h"
#include "rvec.h"
#include "mac_registers.h"
#include "async.h"
#include "timer.h"
#include "res_manager.h"
#include "session.h"

#include "molcpu.h"

// gRVECtable is also accessed mainloop.S
rvec_table_t gRVECtable[NUM_RVECS];

// Assembly exports (mainloop.S)
extern void mainloop_asm_init( void );
extern int mainloop( int rvec, mac_regs_t *m, ulong session_magic );

static int rvec_bad_vector( int vnum );
static int rvec_internal_error( int vnum, int err );
static int rvec_debugger( int vnum, int num );
static int rvec_interrupt( int vnum );

static void print_rvec_stats( int is_cleanup );

static int cmd_rvecs( int, char ** );
static int cmd_ks( int, char ** );

/************************************************************************/
/*	F U N C T I O N S						*/
/************************************************************************/

void
mainloop_init( void )
{
	int i;

	CLEAR( gRVECtable );
	for(i=0; i<NUM_RVECS; i++ )
		set_rvector( i, rvec_bad_vector, NULL, kFPU_Safe );

	set_rvector( RVEC_INTERNAL_ERROR, rvec_internal_error, "Internal Error", kFPU_Unsafe );
	set_rvector( RVEC_DEBUGGER, rvec_debugger, "Debugger", kFPU_Unsafe );
	set_rvector( RVEC_INTERRUPT, rvec_interrupt, "Interrupt", kFPU_Unsafe );

	add_cmd( "rvecs", "rvecs \nShow RVEC statistics\n", -1, cmd_rvecs );
	add_cmd( "ks", "ks \nShow kernel performance statistics\n", -1, cmd_ks );

	mainloop_asm_init();
}

void
mainloop_cleanup( void )
{
	// print_rvec_stats(1);
}

void 
set_rvector( int vnum, void *vector, char *dbg_str, int fpu_safe )
{
	assert( (vnum & RVEC_MASK) < NUM_RVECS && (vnum & RVEC_MASK) >= 0 );
	assert( (fpu_safe == kFPU_Safe) == !(vnum & RVF_FPU_UNSAFE) );
	
	vnum &= RVEC_MASK;
	gRVECtable[vnum].rvec = vector;
	gRVECtable[vnum].dbg_name = dbg_str;
}


/************************************************************************/
/*	Mainloop Control						*/
/************************************************************************/

static int __stop;
enum{ kGo=0, kStop=1, kQuit=-1, kSaveSession=-2 };

void
stop_emulation( void )
{
	if( debugger_attached() ) {
		__stop = kStop;
		interrupt_emulation();
	}
}
void
save_session( void )
{
	__stop = kSaveSession;
}
void 
resume_emulation( void ) 
{
	__stop = kGo;
}
void
quit_emulation( void )
{
	__stop = kQuit;
}

void
mainloop_start( void )
{
	int err;
	_spr_changed();

	/* RVEC_INITIALIZE initializes the low-level kernel parts */
	mainloop( RVEC_INITIALIZE, mregs, g_session_magic );

	__stop = (get_bool_res("debug_stop") == 1);

	while( __stop != kQuit ) {
		switch( __stop ){
		case kGo:
			mainloop( RVEC_NOP, mregs, g_session_magic );
			assert( __stop );
			break;

		case kStop:
			refresh_debugger();
			while( __stop == kStop ) {
				while( mregs->interrupt )
					rvec_interrupt( RVEC_INTERRUPT );
				usleep(1);
			}
			break;

		case kSaveSession:
			printm("Saving the session...\n");
			if( !(err = save_session_now()) ) {
				printm("The session was saved successfully\n");
				__stop = kQuit;
			} else if( err == 1 ){
				static int tries=0;
				__stop = kGo;
				if( ++tries < 200 ) {
					schedule_save_session(1000);
				} else {
					printm("Did not reach a state where the session could be saved\n");
					tries=0;
				}
			} else {
				__stop = kGo;
			}
			break;
		}
	}
	printm("exiting...\n");
}


/************************************************************************/
/*	emulation interrupts						*/
/************************************************************************/

void 
interrupt_emulation( void )
{
	// printm("interrupt_emulation()\n");

	assert( mregs );

	mregs->interrupt = 1; 
	abort_doze();

	// XXX: On SMP we must signal the main thread (this presumably 
	// triggers an IPI which will cause MOL to stop processing 
	// mac instructions).
}

/* We come here as soon as mregs->interrupt is set (or by the kernel
 * code returning RVEC_INTERRUPT). This mechanism is used to handle 
 * a lot of things.
 */
static int
rvec_interrupt( int dummy_rvec )
{
	int flags;
	mregs->interrupt = 0;
	flags = mregs->flag_bits;

	if( mregs->kernel_dbg_stop ) {
		printm("Kernel stop emulation (kernel_dbg_stop = %08lX)\n", mregs->kernel_dbg_stop );
		mregs->kernel_dbg_stop = 0;
		__stop = kStop;
	}

	clear_abort_doze();
	do_async_io();
	do_timer_stuff( (flags & fb_TimerINT) );

	if( (mregs->msr & MSR_EE) ) {
		if( mregs->irq )
			irq_exception();
		else if( flags & fb_DecINT ) {
			//printm("DEC %08lX %08lX\n", mregs->nip, mregs->dec_stamp - get_tbl());
			dec_exception();
		}
	}
	return __stop ? kRVecExit : 0;
}

/************************************************************************/
/*	Misc Return Vectors						*/
/************************************************************************/

static int
rvec_bad_vector( int rvec )
{
	printm("Uninitialized rvector %d occured (%X)\n", rvec & RVEC_MASK, rvec );
	quit_emulation();
	return kRVecExit;
}

static int
rvec_debugger( int dummy_rvec, int num )
{
	printm("RVEC-DEBUGGER <%x>\n", num );
	stop_emulation();
	return kRVecExit;
}

static int
rvec_internal_error( int dummy_rvec, int err )
{
	if( err == 0x1700 ){
		printm("==================================================\n"
		       "A problem related to TAU-interrupts has occured.\n"
		       "This is probably a hardware problem - please turn\n"
		       "off the kernel config option\n"
		       "    Interrupt driven TAU driver (CONFIG_TAU_INT)\n"
		       "\nor\n"
		       "    Thermap Management Support (CONFIG_TAU)\n"
		       "(a kernel recompilation is necessary...)\n"
		       "==================================================\n");
	}
	printm("RVEC Internal Error %x\n", err );
	quit_emulation();
	return kRVecExit;
}


/************************************************************************/
/*	Debugger							*/
/************************************************************************/

static void
print_rvec_stats( int is_cleanup )
{
#ifdef COLLECT_RVEC_STATISTICS
	rvec_table_t *p = gRVECtable;
	int i;	

	for(i=0; i<NUM_RVECS; i++, p++ ) {
		if( p->rvec == rvec_bad_vector )
			continue;
		printm("RVEC %02d %-26s: %d\n",i, p->dbg_name ? p->dbg_name : "----", p->dbg_count );
	}
#else
	if( !is_cleanup ) {
		printm("No RVEC statistics is collected\n");
		printm("MOL was not compiled with COLLECT_RVEC_STATISTICS\n");
	}
#endif

}

static int 
cmd_rvecs( int argc, char **argv )
{
	if( argc != 1 )
		return 1;
	print_rvec_stats(0);
	return 0;
}

static int 
cmd_ks( int argc, char **argv )
{
	int c,i=0;
	char buf[200];

	while( !_get_performance_info(i++,buf,sizeof(buf),&c) )
		printm("%-30s: %d\n", buf, c );
	return 0;
}