2 ; Low level interrupt/thread handling code for GeekOS.
3 ; Copyright (c) 2001,2003,2004 David H. Hovemeyer <daveho@cs.umd.edu>
4 ; Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
7 ; This is free software. You are permitted to use,
8 ; redistribute, and modify it as specified in the file "COPYING".
10 ; This is 32 bit code to be linked into the kernel.
11 ; It defines low level interrupt handler entry points that we'll use
12 ; to populate the IDT. It also contains the interrupt handling
13 ; and thread context switch code.
22 ; ----------------------------------------------------------------------
24 ; ----------------------------------------------------------------------
26 ; This is the size of the Interrupt_State struct in int.h
27 INTERRUPT_STATE_SIZE equ 64
29 ; Save registers prior to calling a handler function.
30 ; This must be kept up to date with:
31 ; - Interrupt_State struct in int.h
32 ; - Setup_Initial_Thread_Context() in kthread.c
33 %macro Save_Registers 0
47 ; Restore registers and clean up the stack after calling a handler function
48 ; (i.e., just before we return from the interrupt via an iret instruction).
49 %macro Restore_Registers 0
61 add esp, 8 ; skip int num and error code
64 ; Code to activate a new user context (if necessary), before returning
65 ; to executing a thread. Should be called just before restoring
66 ; registers (because the interrupt context is used).
67 %macro Activate_User_Context 0
68 ; If the new thread has a user context which is not the current
70 push esp ; Interrupt_State pointer
71 push dword [g_currentThread] ; Kernel_Thread pointer
72 ; call Switch_To_User_Context
73 add esp, 8 ; clear 2 arguments
76 ; Number of bytes between the top of the stack and
77 ; the interrupt number after the general-purpose and segment
78 ; registers have been saved.
81 ; Template for entry point code for interrupts that have
82 ; an explicit processor-generated error code.
83 ; The argument is the interrupt number.
86 push dword %1 ; push interrupt number
87 jmp Handle_Interrupt ; jump to common handler
90 ; Template for entry point code for interrupts that do not
91 ; generate an explicit error code. We push a dummy error
92 ; code on the stack, so the stack layout is the same
96 push dword 0 ; fake error code
97 push dword %1 ; push interrupt number
98 jmp Handle_Interrupt ; jump to common handler
102 ; ----------------------------------------------------------------------
103 ; Symbol imports and exports
104 ; ----------------------------------------------------------------------
106 ; This symbol is defined in idt.c, and is a table of addresses
107 ; of C handler functions for interrupts.
108 IMPORT g_interruptTable
110 ; Global variable pointing to context struct for current thread.
111 IMPORT g_currentThread
113 ; Set to non-zero when we need to choose a new thread
114 ; in the interrupt return code.
115 IMPORT g_needReschedule
117 ; Set to non-zero when preemption is disabled.
118 IMPORT g_preemptionDisabled
120 ; This is the function that returns the next runnable thread.
121 IMPORT Get_Next_Runnable
123 ; Function to put a thread on the run queue.
126 ; Function to activate a new user context (if needed).
127 IMPORT Switch_To_User_Context
130 IMPORT SerialPrintHex
133 ; Sizes of interrupt handler entry points for interrupts with
134 ; and without error codes. The code in idt.c uses this
135 ; information to infer the layout of the table of interrupt
136 ; handler entry points, without needing a separate linker
137 ; symbol for each one (which is quite tedious to type :-)
138 EXPORT g_handlerSizeNoErr
139 EXPORT g_handlerSizeErr
141 ; Simple functions to load the IDTR, GDTR, and LDTR.
146 ; Beginning and end of the table of interrupt entry points.
147 EXPORT g_entryPointTableStart
148 EXPORT g_entryPointTableEnd
150 ; Thread context switch function.
151 EXPORT Switch_To_Thread
153 ; Return current value of eflags register.
154 EXPORT Get_Current_EFLAGS
156 ; Return the return address
163 ; Virtual memory support.
185 ; ----------------------------------------------------------------------
187 ; ----------------------------------------------------------------------
191 ; Load IDTR with 6-byte pointer whose address is passed as
199 ; Load the GDTR with 6-byte pointer whose address is
200 ; passed as the parameter. Assumes that interrupts
206 ; Reload segment registers
217 ; Load the LDT whose selector is passed as a parameter.
226 ; load crt3 with the passed page directory pointer
227 ; enable paging bit in cr2
252 ; load cr3 with the passed page directory pointer
260 ; Get the current PDBR.
261 ; This is useful for lazily switching address spaces;
262 ; only switch if the new thread has a different address space.
270 ; Flush TLB - just need to re-load cr3 to force this to happen
281 ; cpuid_edx - return the edx register from cpuid
303 ; cpuid_ecx - return the ecx register from cpuid
324 ; cpuid_eax - return the eax register from cpuid
344 ; Set_MSR - Set the value of a given MSR
362 ; Get_MSR - Get the value of a given MSR
363 ; void Get_MSR(int MSR, void * high_byte, void * low_byte);
408 ; Common interrupt handling code.
409 ; Save registers, call C handler function,
410 ; possibly choose a new thread to run, restore
411 ; registers, return from the interrupt.
414 ; Save registers (general purpose and segment)
417 ; Ensure that we're using the kernel data segment
422 ; Get the address of the C handler function from the
423 ; table of handler functions.
424 mov eax, g_interruptTable ; get address of handler table
425 mov esi, [esp+REG_SKIP] ; get interrupt number
426 mov ebx, [eax+esi*4] ; get address of handler function
429 ; call SerialPrintHex
440 ; The argument passed is a pointer to an Interrupt_State struct,
441 ; which describes the stack layout for all interrupts.
444 add esp, 4 ; clear 1 argument
446 ; If preemption is disabled, then the current thread
448 cmp [g_preemptionDisabled], dword 0
451 ; See if we need to choose a new thread to run.
452 cmp [g_needReschedule], dword 0
455 ; Put current thread back on the run queue
456 push dword [g_currentThread]
458 add esp, 4 ; clear 1 argument
460 ; Save stack pointer in current thread context, and
461 ; clear numTicks field.
462 mov eax, [g_currentThread]
463 mov [eax+0], esp ; esp field
464 mov [eax+4], dword 0 ; numTicks field
466 ; Pick a new thread to run, and switch to its stack
467 call Get_Next_Runnable
468 mov [g_currentThread], eax
469 mov esp, [eax+0] ; esp field
471 ; Clear "need reschedule" flag
472 mov [g_needReschedule], dword 0
493 ; mov eax, [ebp + ecx]
496 ; call SerialPrintHex
512 ; Activate the user context, if necessary.
513 Activate_User_Context
523 ; call SerialPrintHex
533 ; Return from the interrupt.
536 ; ----------------------------------------------------------------------
538 ; Save context of currently executing thread, and activate
539 ; the thread whose context object is passed as a parameter.
542 ; - ptr to Kernel_Thread whose state should be restored and made active
545 ; Called with interrupts disabled.
546 ; This must be kept up to date with definition of Kernel_Thread
547 ; struct, in kthread.h.
548 ; ----------------------------------------------------------------------
551 ; Modify the stack to allow a later return via an iret instruction.
552 ; We start with a stack that looks like this:
555 ; esp --> return addr
557 ; We change it to look like this:
562 ; esp --> return addr
565 mov eax, [esp+4] ; get return address
566 mov [esp-4], eax ; move return addr down 8 bytes from orig loc
567 add esp, 8 ; move stack ptr up
568 pushfd ; put eflags where return address was
569 mov eax, [esp-4] ; restore saved value of eax
570 push dword KERNEL_CS ; push cs selector
571 sub esp, 4 ; point stack ptr at return address
573 ; Push fake error code and interrupt number
577 ; Save general purpose registers.
580 ; Save stack pointer in the thread context struct (at offset 0).
581 mov eax, [g_currentThread]
584 ; Clear numTicks field in thread context, since this
585 ; thread is being suspended.
588 ; Load the pointer to the new thread context into eax.
589 ; We skip over the Interrupt_State struct on the stack to
591 mov eax, [esp+INTERRUPT_STATE_SIZE]
593 ; Make the new thread current, and switch to its stack.
594 mov [g_currentThread], eax
597 ; Activate the user context, if necessary.
598 Activate_User_Context
600 ; Restore general purpose and segment registers, and clear interrupt
601 ; number and error code.
604 ; We'll return to the place where the thread was
608 ; Return current contents of eflags register.
612 pop eax ; pop contents into eax
617 ; Return the eip of the next instruction after the caller
623 ; Return the current esp in the procedure
629 ; Return the current ebp in the procedure
637 ; ----------------------------------------------------------------------
638 ; Generate interrupt-specific entry points for all interrupts.
639 ; We also define symbols to indicate the extend of the table
640 ; of entry points, and the size of individual entry points.
641 ; ----------------------------------------------------------------------
643 g_entryPointTableStart:
645 ; Handlers for processor-generated exceptions, as defined by
653 Int_No_Err 2 ; FIXME: not described in 486 manual
664 Int_No_Err 9 ; FIXME: not described in 486 manual
670 Int_No_Err 15 ; FIXME: not described in 486 manual
674 ; The remaining interrupts (18 - 255) do not have error codes.
675 ; We can generate them all in one go with nasm's %rep construct.
679 %assign intNum intNum+1
683 g_entryPointTableEnd:
687 ; Exported symbols defining the size of handler entry points
688 ; (both with and without error codes).
690 g_handlerSizeNoErr: dd (After_No_Err - Before_No_Err)
692 g_handlerSizeErr: dd (After_Err - Before_Err)