Subversion Repositories HelenOS-historic

Rev

Rev 915 | Rev 919 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 vana 1
#
2
# Copyright (C) 2005 Jakub Vana
478 jermar 3
# Copyright (C) 2005 Jakub Jermar
212 vana 4
# All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
8
# are met:
9
#
10
# - Redistributions of source code must retain the above copyright
11
#   notice, this list of conditions and the following disclaimer.
12
# - Redistributions in binary form must reproduce the above copyright
13
#   notice, this list of conditions and the following disclaimer in the
14
#   documentation and/or other materials provided with the distribution.
15
# - The name of the author may not be used to endorse or promote products
16
#   derived from this software without specific prior written permission.
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#
29
 
443 jermar 30
#include <arch/stack.h>
478 jermar 31
#include <arch/register.h>
912 jermar 32
#include <arch/mm/page.h>
33
#include <align.h>
212 vana 34
 
915 jermar 35
#define STACK_ITEMS		14
912 jermar 36
#define STACK_FRAME_SIZE	ALIGN_UP((STACK_ITEMS*STACK_ITEM_SIZE) + STACK_SCRATCH_AREA_SIZE, STACK_ALIGNMENT)
443 jermar 37
 
912 jermar 38
#if (STACK_ITEMS % 2 == 0)
39
#	define STACK_FRAME_BIAS	8
40
#else
41
#	define STACK_FRAME_BIAS 16
443 jermar 42
#endif
43
 
911 jermar 44
/** Partitioning of bank 0 registers. */
45
#define R_OFFS 		r16
46
#define R_HANDLER	r17
47
#define R_RET		r18
916 jermar 48
#define R_KSTACK_BSP	r22	/* keep in sync with before_thread_runs_arch() */
911 jermar 49
#define R_KSTACK	r23	/* keep in sync with before_thread_runs_arch() */
50
 
438 jermar 51
/** Heavyweight interrupt handler
52
 *
435 jermar 53
 * This macro roughly follows steps from 1 to 19 described in
54
 * Intel Itanium Architecture Software Developer's Manual, Chapter 3.4.2.
55
 *
438 jermar 56
 * HEAVYWEIGHT_HANDLER macro must cram into 16 bundles (48 instructions).
57
 * This goal is achieved by using procedure calls after RSE becomes operational.
58
 *
435 jermar 59
 * Some steps are skipped (enabling and disabling interrupts).
916 jermar 60
 * Some steps are not fully supported yet (e.g. dealing with floating-point
61
 * context).
456 jermar 62
 *
63
 * @param offs Offset from the beginning of IVT.
64
 * @param handler Interrupt handler address.
435 jermar 65
 */
470 jermar 66
.macro HEAVYWEIGHT_HANDLER offs, handler=universal_handler
67
    .org ivt + \offs
911 jermar 68
	mov R_OFFS = \offs
69
	movl R_HANDLER = \handler ;;
470 jermar 70
	br heavyweight_handler
71
.endm
212 vana 72
 
470 jermar 73
.global heavyweight_handler
74
heavyweight_handler:
435 jermar 75
    /* 1. copy interrupt registers into bank 0 */
911 jermar 76
 
77
	/*
912 jermar 78
	 * Note that r24-r31 from bank 0 can be used only as long as PSR.ic = 0.
911 jermar 79
	 */
435 jermar 80
	mov r24 = cr.iip
81
	mov r25 = cr.ipsr
82
	mov r26 = cr.iipa
83
	mov r27 = cr.isr
84
	mov r28 = cr.ifa
85
 
86
    /* 2. preserve predicate register into bank 0 */
87
	mov r29 = pr ;;
88
 
438 jermar 89
    /* 3. switch to kernel memory stack */
912 jermar 90
    	mov r30 = cr.ipsr
916 jermar 91
	shr.u r31 = r12, VRN_SHIFT ;;
912 jermar 92
 
916 jermar 93
	shr.u r30 = r30, PSR_CPL_SHIFT ;;
94
	and r30 = PSR_CPL_MASK_SHIFTED, r30 ;;
95
 
912 jermar 96
	/*
916 jermar 97
	 * Set p3 to true if the interrupted context executed in kernel mode.
98
	 * Set p4 to false if the interrupted context didn't execute in kernel mode.
912 jermar 99
	 */
916 jermar 100
	cmp.eq p3, p4 = r30, r0 ;;
101
	cmp.eq p1, p2 = r30, r0 ;;	/* remember IPSR setting in p1 and p2 */
912 jermar 102
 
103
	/*
916 jermar 104
	 * Set p3 to true if the stack register references kernel address space.
105
	 * Set p4 to false if the stack register doesn't reference kernel address space.
912 jermar 106
	 */
916 jermar 107
	(p3) cmp.eq p3, p4 = VRN_KERNEL, r31 ;;
912 jermar 108
 
109
	/*
916 jermar 110
	 * Now, p4 is true iff the stack needs to be switched to kernel stack.
912 jermar 111
	 */
112
	mov r30 = r12
916 jermar 113
	(p4) mov r12 = R_KSTACK ;;
912 jermar 114
 
115
	add r31 = -STACK_FRAME_BIAS, r12 ;;
470 jermar 116
	add r12 = -STACK_FRAME_SIZE, r12
117
 
118
    /* 4. save registers in bank 0 into memory stack */	
912 jermar 119
	st8 [r31] = r30, -8 ;;		/* save old stack pointer */ 
120
 
121
	st8 [r31] = r29, -8 ;;		/* save predicate registers */
438 jermar 122
 
912 jermar 123
	st8 [r31] = r24, -8 ;;		/* save cr.iip */
124
	st8 [r31] = r25, -8 ;;		/* save cr.ipsr */
125
	st8 [r31] = r26, -8 ;;		/* save cr.iipa */
126
	st8 [r31] = r27, -8 ;;		/* save cr.isr */
127
	st8 [r31] = r28, -8 ;;		/* save cr.ifa */
438 jermar 128
 
129
    /* 5. RSE switch from interrupted context */
435 jermar 130
	mov r24 = ar.rsc
131
	mov r25 = ar.pfs
132
	cover
133
	mov r26 = cr.ifs
134
 
916 jermar 135
	st8 [r31] = r24, -8 ;;		/* save ar.rsc */
136
	st8 [r31] = r25, -8 ;;		/* save ar.pfs */
137
	st8 [r31] = r26, -8		/* save ar.ifs */
435 jermar 138
 
470 jermar 139
	and r30 = ~3, r24 ;;
916 jermar 140
	mov ar.rsc = r30 ;;		/* place RSE in enforced lazy mode */
435 jermar 141
 
142
	mov r27 = ar.rnat
470 jermar 143
	mov r28 = ar.bspstore ;;
435 jermar 144
 
916 jermar 145
	/*
146
	 * Inspect BSPSTORE to figure out whether it is necessary to switch to kernel BSPSTORE.
147
	 */
148
	(p1) shr.u r30 = r28, VRN_SHIFT ;;
149
	(p1) cmp.eq p1, p2 = VRN_KERNEL, r30 ;;
435 jermar 150
 
916 jermar 151
	/*
152
	 * If BSPSTORE needs to be switched, p1 is false and p2 is true.
153
	 */
154
	(p1) mov r30 = r28
155
	(p2) mov r30 = R_KSTACK_BSP ;;
156
	(p2) mov ar.bspstore = r30 ;;
157
 
435 jermar 158
	mov r29 = ar.bsp
159
 
916 jermar 160
	st8 [r31] = r27, -8 ;;		/* save ar.rnat */
161
	st8 [r31] = r30, -8 ;;		/* save new value written to ar.bspstore */
162
	st8 [r31] = r28, -8 ;;		/* save ar.bspstore */
163
	st8 [r31] = r29, -8 		/* save ar.bsp */
435 jermar 164
 
916 jermar 165
	mov ar.rsc = r24		/* restore RSE's setting */
435 jermar 166
 
470 jermar 167
    /* steps 6 - 15 are done by heavyweight_handler_inner() */
916 jermar 168
	mov R_RET = b0 			/* save b0 belonging to interrupted context */
911 jermar 169
	br.call.sptk.many b0 = heavyweight_handler_inner
916 jermar 170
0:	mov b0 = R_RET			/* restore b0 belonging to the interrupted context */
438 jermar 171
 
470 jermar 172
    /* 16. RSE switch to interrupted context */
916 jermar 173
	cover				/* allocate zerro size frame (step 1 (from Intel Docs)) */
438 jermar 174
 
470 jermar 175
	add r31 = STACK_SCRATCH_AREA_SIZE, r12 ;;
176
 
915 jermar 177
	ld8 r30 = [r31], +8 ;;		/* load ar.bsp */
178
	ld8 r29 = [r31], +8 ;;   	/* load ar.bspstore */
179
	ld8 r28 = [r31], +8 ;;   	/* load ar.bspstore_new */
180
	sub r27 = r30 , r28 ;;		/* calculate loadrs (step 2) */
470 jermar 181
	shl r27 = r27, 16
182
 
183
	mov r24 = ar.rsc ;;
184
	and r30 = ~3, r24 ;;
185
	or  r24 = r30 , r27 ;;     
186
	mov ar.rsc = r24 ;;		/* place RSE in enforced lazy mode */
187
 
188
	loadrs 				/* (step 3) */
189
 
190
	ld8 r27 = [r31], +8 ;;		/* load ar.rnat */
191
	ld8 r26 = [r31], +8 ;;		/* load cr.ifs */
192
	ld8 r25 = [r31], +8 ;;		/* load ar.pfs */
193
	ld8 r24 = [r31], +8 ;;		/* load ar.rsc */
194
 
915 jermar 195
	mov ar.bspstore = r29 ;;	/* (step 4) */
196
	mov ar.rnat = r27		/* (step 5) */
470 jermar 197
 
198
	mov ar.pfs = r25		/* (step 6) */
199
	mov cr.ifs = r26	
200
 
201
	mov ar.rsc = r24		/* (step 7) */
202
 
203
    /* 17. restore interruption state from memory stack */
916 jermar 204
	ld8 r28 = [r31], +8 ;;		/* load cr.ifa */		
205
	ld8 r27 = [r31], +8 ;;		/* load cr.isr */
206
	ld8 r26 = [r31], +8 ;;		/* load cr.iipa */
207
	ld8 r25 = [r31], +8 ;;		/* load cr.ipsr */
208
	ld8 r24 = [r31], +8 ;;		/* load cr.iip */
470 jermar 209
 
210
	mov cr.iip = r24
211
	mov cr.ipsr = r25
212
	mov cr.iipa = r26
213
	mov cr.isr = r27
214
	mov cr.ifa = r28
215
 
216
    /* 18. restore predicate registers from memory stack */
916 jermar 217
	ld8 r29 = [r31], +8 ;;		/* load predicate registers */
470 jermar 218
	mov pr = r29
219
 
220
    /* 19. return from interruption */
916 jermar 221
    	ld8 r12 = [r31]			/* load stack pointer */ 
470 jermar 222
	rfi ;;
223
 
438 jermar 224
.global heavyweight_handler_inner
225
heavyweight_handler_inner:
226
	/*
227
	 * From this point, the rest of the interrupted context
228
	 * will be preserved in stacked registers and backing store.
229
	 */
470 jermar 230
	alloc loc0 = ar.pfs, 0, 47, 2, 0 ;;
438 jermar 231
 
470 jermar 232
	/* bank 0 is going to be shadowed, copy essential data from there */
911 jermar 233
	mov loc1 = R_RET	/* b0 belonging to interrupted context */
234
	mov loc2 = R_HANDLER
235
	mov out0 = R_OFFS
470 jermar 236
 
237
	add out1 = STACK_SCRATCH_AREA_SIZE, r12
438 jermar 238
 
435 jermar 239
    /* 6. switch to bank 1 and reenable PSR.ic */
478 jermar 240
	ssm PSR_IC_MASK
435 jermar 241
	bsw.1 ;;
242
	srlz.d
243
 
244
    /* 7. preserve branch and application registers */
470 jermar 245
    	mov loc3 = ar.unat
246
	mov loc4 = ar.lc
247
	mov loc5 = ar.ec
248
	mov loc6 = ar.ccv
249
	mov loc7 = ar.csd
250
	mov loc8 = ar.ssd
435 jermar 251
 
470 jermar 252
	mov loc9 = b0
253
	mov loc10 = b1
254
	mov loc11 = b2
255
	mov loc12 = b3
256
	mov loc13 = b4
257
	mov loc14 = b5
258
	mov loc15 = b6
259
	mov loc16 = b7
438 jermar 260
 
435 jermar 261
    /* 8. preserve general and floating-point registers */
262
	/* TODO: save floating-point context */
470 jermar 263
	mov loc17 = r1
264
	mov loc18 = r2
265
	mov loc19 = r3
266
	mov loc20 = r4
267
	mov loc21 = r5
268
	mov loc22 = r6
269
	mov loc23 = r7
270
	mov loc24 = r8
271
	mov loc25 = r9
272
	mov loc26 = r10
273
	mov loc27 = r11
438 jermar 274
	/* skip r12 (stack pointer) */
470 jermar 275
	mov loc28 = r13
276
	mov loc29 = r14
277
	mov loc30 = r15
278
	mov loc31 = r16
279
	mov loc32 = r17
280
	mov loc33 = r18
281
	mov loc34 = r19
282
	mov loc35 = r20
283
	mov loc36 = r21
284
	mov loc37 = r22
285
	mov loc38 = r23
286
	mov loc39 = r24
287
	mov loc40 = r25
288
	mov loc41 = r26
289
	mov loc42 = r27
290
	mov loc43 = r28
291
	mov loc44 = r29
292
	mov loc45 = r30
293
	mov loc46 = r31
438 jermar 294
 
435 jermar 295
    /* 9. skipped (will not enable interrupts) */
478 jermar 296
	/*
297
    	 * ssm PSR_I_MASK
298
	 * ;;
299
	 * srlz.d
300
	 */
238 vana 301
 
438 jermar 302
    /* 10. call handler */
470 jermar 303
    	mov b1 = loc2
438 jermar 304
	br.call.sptk.many b0 = b1
305
 
306
    /* 11. return from handler */
307
0:
308
 
435 jermar 309
    /* 12. skipped (will not disable interrupts) */
478 jermar 310
	/*
311
    	 * rsm PSR_I_MASK
312
	 * ;;
313
	 * srlz.d
314
	 */
438 jermar 315
 
435 jermar 316
    /* 13. restore general and floating-point registers */
317
	/* TODO: restore floating-point context */
470 jermar 318
	mov r1 = loc17
319
	mov r2 = loc18
320
	mov r3 = loc19
321
	mov r4 = loc20
322
	mov r5 = loc21
323
	mov r6 = loc22
324
	mov r7 = loc23
325
	mov r8 = loc24
326
	mov r9 = loc25
327
	mov r10 = loc26
328
	mov r11 = loc27
438 jermar 329
	/* skip r12 (stack pointer) */
470 jermar 330
	mov r13 = loc28
331
	mov r14 = loc29
332
	mov r15 = loc30
333
	mov r16 = loc31
334
	mov r17 = loc32
335
	mov r18 = loc33
336
	mov r19 = loc34
337
	mov r20 = loc35
338
	mov r21 = loc36
339
	mov r22 = loc37
340
	mov r23 = loc38
341
	mov r24 = loc39
342
	mov r25 = loc40
343
	mov r26 = loc41 
344
	mov r27 = loc42
345
	mov r28 = loc43
346
	mov r29 = loc44
347
	mov r30 = loc45
348
	mov r31 = loc46
435 jermar 349
 
350
    /* 14. restore branch and application registers */
470 jermar 351
    	mov ar.unat = loc3
352
	mov ar.lc = loc4
353
	mov ar.ec = loc5
354
	mov ar.ccv = loc6
355
	mov ar.csd = loc7
356
	mov ar.ssd = loc8
435 jermar 357
 
470 jermar 358
	mov b0 = loc9
359
	mov b1 = loc10
360
	mov b2 = loc11
361
	mov b3 = loc12
362
	mov b4 = loc13
363
	mov b5 = loc14
364
	mov b6 = loc15
365
	mov b7 = loc16
438 jermar 366
 
435 jermar 367
    /* 15. disable PSR.ic and switch to bank 0 */
478 jermar 368
	rsm PSR_IC_MASK
435 jermar 369
	bsw.0 ;;
370
	srlz.d
438 jermar 371
 
911 jermar 372
	mov R_RET = loc1
438 jermar 373
	mov ar.pfs = loc0
470 jermar 374
	br.ret.sptk.many b0
438 jermar 375
 
470 jermar 376
.global ivt
377
.align 32768
378
ivt:
379
	HEAVYWEIGHT_HANDLER 0x0000
380
	HEAVYWEIGHT_HANDLER 0x0400
381
	HEAVYWEIGHT_HANDLER 0x0800
899 jermar 382
	HEAVYWEIGHT_HANDLER 0x0c00 alternate_instruction_tlb_fault
383
	HEAVYWEIGHT_HANDLER 0x1000 alternate_data_tlb_fault
384
	HEAVYWEIGHT_HANDLER 0x1400 data_nested_tlb_fault
470 jermar 385
	HEAVYWEIGHT_HANDLER 0x1800
386
	HEAVYWEIGHT_HANDLER 0x1c00
899 jermar 387
	HEAVYWEIGHT_HANDLER 0x2000 data_dirty_bit_fault
388
	HEAVYWEIGHT_HANDLER 0x2400 instruction_access_bit_fault
389
	HEAVYWEIGHT_HANDLER 0x2800 data_access_bit_fault
470 jermar 390
	HEAVYWEIGHT_HANDLER 0x2c00 break_instruction
391
	HEAVYWEIGHT_HANDLER 0x3000 external_interrupt	/* For external interrupt, heavyweight handler is used. */
392
	HEAVYWEIGHT_HANDLER 0x3400
393
	HEAVYWEIGHT_HANDLER 0x3800
394
	HEAVYWEIGHT_HANDLER 0x3c00
395
	HEAVYWEIGHT_HANDLER 0x4000
396
	HEAVYWEIGHT_HANDLER 0x4400
397
	HEAVYWEIGHT_HANDLER 0x4800
398
	HEAVYWEIGHT_HANDLER 0x4c00
444 vana 399
 
899 jermar 400
	HEAVYWEIGHT_HANDLER 0x5000 page_not_present
470 jermar 401
	HEAVYWEIGHT_HANDLER 0x5100
402
	HEAVYWEIGHT_HANDLER 0x5200
403
	HEAVYWEIGHT_HANDLER 0x5300
404
	HEAVYWEIGHT_HANDLER 0x5400 general_exception
405
	HEAVYWEIGHT_HANDLER 0x5500
406
	HEAVYWEIGHT_HANDLER 0x5600
407
	HEAVYWEIGHT_HANDLER 0x5700
408
	HEAVYWEIGHT_HANDLER 0x5800
409
	HEAVYWEIGHT_HANDLER 0x5900
410
	HEAVYWEIGHT_HANDLER 0x5a00
411
	HEAVYWEIGHT_HANDLER 0x5b00
412
	HEAVYWEIGHT_HANDLER 0x5c00
413
	HEAVYWEIGHT_HANDLER 0x5d00
414
	HEAVYWEIGHT_HANDLER 0x5e00
415
	HEAVYWEIGHT_HANDLER 0x5f00
435 jermar 416
 
470 jermar 417
	HEAVYWEIGHT_HANDLER 0x6000
418
	HEAVYWEIGHT_HANDLER 0x6100
419
	HEAVYWEIGHT_HANDLER 0x6200
420
	HEAVYWEIGHT_HANDLER 0x6300
421
	HEAVYWEIGHT_HANDLER 0x6400
422
	HEAVYWEIGHT_HANDLER 0x6500
423
	HEAVYWEIGHT_HANDLER 0x6600
424
	HEAVYWEIGHT_HANDLER 0x6700
425
	HEAVYWEIGHT_HANDLER 0x6800
426
	HEAVYWEIGHT_HANDLER 0x6900
427
	HEAVYWEIGHT_HANDLER 0x6a00
428
	HEAVYWEIGHT_HANDLER 0x6b00
429
	HEAVYWEIGHT_HANDLER 0x6c00
430
	HEAVYWEIGHT_HANDLER 0x6d00
431
	HEAVYWEIGHT_HANDLER 0x6e00
432
	HEAVYWEIGHT_HANDLER 0x6f00
435 jermar 433
 
470 jermar 434
	HEAVYWEIGHT_HANDLER 0x7000
435
	HEAVYWEIGHT_HANDLER 0x7100
436
	HEAVYWEIGHT_HANDLER 0x7200
437
	HEAVYWEIGHT_HANDLER 0x7300
438
	HEAVYWEIGHT_HANDLER 0x7400
439
	HEAVYWEIGHT_HANDLER 0x7500
440
	HEAVYWEIGHT_HANDLER 0x7600
441
	HEAVYWEIGHT_HANDLER 0x7700
442
	HEAVYWEIGHT_HANDLER 0x7800
443
	HEAVYWEIGHT_HANDLER 0x7900
444
	HEAVYWEIGHT_HANDLER 0x7a00
445
	HEAVYWEIGHT_HANDLER 0x7b00
446
	HEAVYWEIGHT_HANDLER 0x7c00
447
	HEAVYWEIGHT_HANDLER 0x7d00
448
	HEAVYWEIGHT_HANDLER 0x7e00
449
	HEAVYWEIGHT_HANDLER 0x7f00