OOStuBS - Technische Informatik II (TI-II)  2.4
boot.S
gehe zur Dokumentation dieser Datei
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2  * Technische Informatik II *
3  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * *
5  * B O O T *
6  * *
7 \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
8 
9 /* Tell includes, that this is an assembly file */
10 #define ASM 1
11 #include <machine/multiboot.h>
12 #include <config.h>
13 
14 
15 .text /* The fallowing is code */
16 
17 
18 .globl entry /* Define the entry point for the bootloader to be exported */
19 
20 
21 .macro wrapperGen /* Assembler macro to create interrupt wrapper */
22  .ifeq \@ /* \@ is an automatically incremented variable, */
23  INT_WRAPPER_START: /* it is incremented, everytime the macro */
24  .endif /* is expanded. */
25 
26  int_wrapper_\@:
27  push %esp /* put current stack pointer as 2. parameter */
28  pushl $\@ /* put interrupt number as 1. parameter */
29  call handleException /* call the unified exception handling function */
30  add $8, %esp /* cleanup stack frame */
31  iret /* end interrupt */
32 
33  .ifeq \@
35  .endif
36 .endm
37 
38 .align 4 /* The Multiboot header must be aligned to 32bit */
39 
40 multiboot_header:
44 #ifndef __ELF__
45  .long multiboot_header
46  .long _start
47  .long _edata
48  .long _end
49  .long entry
50 #endif
51 
52 entry: /* Entry point of the system */
53  cli
54 
55 gdtSetup: /* Initialize the GDT */
56  lgdt GDTR /* Load the static GDT */
57 
58  mov $0x10, %cx /* Setting the segment selectors*/
59  mov %cx, %ss /* stack is data*/
60  mov %cx, %ds
61  mov %cx, %es
62  mov %cx, %gs
63  mov %cx, %fs
64  ljmpl $0x08, $stackSetup /* set cs register to 0x08 */
65 
66 stackSetup: /*Creating a stack*/
67  mov $(stack + BOOT_STACK_SIZE), %esp
68 
69 multibootSetup: /* Passing multiboot arguments to kernel */
70  pushl %ebx
71  pushl %eax
72 
73 idtSetup:
74  mov $initialIDT, %eax /* Load the base address of the IDT template */
75  mov $256, %dx /* There are 256 possible interrupts */
76  movl $(int_wrapper_0-INT_WRAPPER_END+INT_WRAPPER_START), %ebx /* Load base address of the wrapper function */
77 
78 .loop:
79  add $(INT_WRAPPER_END-INT_WRAPPER_START), %ebx /* Load the address of the current wrapper function */
80  movl %ebx, %ecx
81  mov %cx, (%eax) /* Copy the lower bytes of the address to the IDT */
82  shr $16, %ecx
83  mov %cx, 6(%eax) /* Copy the upper bytes of the address to the IDT */
84  add $8, %eax
85  dec %dx
86  jnz .loop
87 
88  lidt IDTR /* Load the IDT */
89 
90 disableNMI: /* Disable non-maskable interrupts */
91  mov $0x80, %al /* NMI verbieten */
92  out %al, $0x70
93 
94 initFPU: /* setup FPU for 486 and newer */
95  pushl %eax
96  mov %CR0, %eax
97  and $(-1-0x04), %eax /* clear CR0.EM Bit */
98  or $0x20, %eax /* set CR0.NE Bit */
99  mov %eax, %CR0
100  popl %eax
101  finit /* initialize floating point */
102  fldcw fpucw_37E /* enable invalid opcode exception */
103  ffree %ST(0)
104  ffree %ST(1)
105  ffree %ST(2)
106  ffree %ST(3)
107  ffree %ST(4)
108  ffree %ST(5)
109  ffree %ST(6)
110  ffree %ST(7)
111 
112 setupObj: /* Call the constructors of global objects */
113  call constructObjects
114 
115 callKernel: /* Call the C/C++ main function of the operating system */
116  call kernel
117 
118 destroyObj: /* Call the destructors of global objects */
119  call destructObjects
120 
121 leaveKernel: /* The operating system ended, halt the CPU */
122  cli
123 .halt: hlt
124  jmp .halt
125 
126 
127 
128 intWrappers: /* Create the interrupt wrappers */
129  .rept 256
130  wrapperGen
131  .endr
132 
133 handleException: /* Unified interrupt handling routine */
134  push %ebp /* create stack frame */
135  mov %esp, %ebp
136 
137  push %ecx /* save volatile registers */
138  push %edx
139  push %eax
140 
141  pushl 12(%ebp) /* forward 2. parameter (stack pointer on exception entry) */
142  pushl 8(%ebp) /* forward 1. parameter (interrupt number) */
143  cld /* expected by gcc */
144  call exception /* call the interrupt handler */
145  add $8, %esp /* cleanup */
146 
147  pop %eax /* retrieve saved registers */
148  pop %edx
149  pop %ecx
150 
151  leave /* cleanup stack frame */
152  ret /* jump back to wrapper */
153 
154 .bss /* the fallowing is uninitialized reserved memory */
155 
156  .comm stack, BOOT_STACK_SIZE /* memory for stack */
157 
158 
159 .data /* pre-initialized memory */
160 
161 fpucw_37E:
162  .short 0x37E /* initial value for FPU */
163 
164 GDTR: /* The gdt pseudo-register */
165  .short 3*8-1 /* 3 segments, null, code, data */
166  .int initialGDT /* start of GDT */
167 
168 IDTR: /* The idt pseudo-register */
169  .short 256*8-1 /* 256 handlers */
170  .int initialIDT /* start if IDT */
171 
172  .align 4
173 
174 initialGDT: /* The fixed GDT for the operating system */
175 
176 nullSegment: /* Null segment as expected by x86 architecture */
177  .short 0
178  .short 0
179  .byte 0
180  .byte 0
181  .byte 0
182  .byte 0
183 
184 codeSegment: /* Code segment, flat memory model, read and execute */
185  .short 0xFFFF
186  .short 0
187  .byte 0
188  .byte 0x9A
189  .byte 0xCF
190  .byte 0
191 
192 dataSegment: /* Data segment, flat memory model, read and write */
193  .short 0xFFFF
194  .short 0
195  .byte 0
196  .byte 0x92
197  .byte 0xCF
198  .byte 0
199 
200 
201 initialIDT: /* The fixed IDT for the initial protected mode setup */
202 
203 traps: /* 32 Trap handler for CPU generated traps(, faults, interr. and aborts) */
204  .rept 32
205  .short 0
206  .short 0x8
207  .byte 0
208  .byte 0xEF
209  .short 0
210  .endr
211 
212 externalInts: /* 224 interrupt handler for possibly external interrupts */
213  .rept 224
214  .short 0
215  .short 0x8
216  .byte 0
217  .byte 0xEE
218  .short 0
219  .endr