Skip to content

Commit fb41d89

Browse files
tomutaawesomekling
authored andcommitted
Kernel: Implement software context switching and Processor structure
Moving certain globals into a new Processor structure for each CPU allows us to eventually run an instance of the scheduler on each CPU.
1 parent 1040706 commit fb41d89

22 files changed

+1002
-513
lines changed

Kernel/Arch/i386/Boot/boot.S

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,6 @@ apic_ap_start:
252252
mov %cs, %ax
253253
mov %ax, %ds
254254

255-
/* Generate a new processor id. This is not the APIC id. We just
256-
need a way to find ourselves a stack without stomping on other
257-
APs that may be doing this concurrently. */
258-
xor %ax, %ax
259-
mov %ax, %bp
260-
inc %ax
261-
lock; xaddw %ax, %ds:(ap_cpu_id - apic_ap_start)(%bp) /* avoid relocation entries */
262-
mov %ax, %bx
263-
264255
xor %ax, %ax
265256
mov %ax, %sp
266257

@@ -281,14 +272,18 @@ apic_ap_start32:
281272
mov %ax, %es
282273
mov %ax, %fs
283274
mov %ax, %gs
284-
275+
285276
movl $0x8000, %ebp
286-
277+
278+
/* generate a unique ap cpu id (0 means 1st ap, not bsp!) */
279+
xorl %eax, %eax
280+
incl %eax
281+
lock; xaddl %eax, (ap_cpu_id - apic_ap_start)(%ebp) /* avoid relocation entries */
282+
movl %eax, %esi
283+
287284
/* find our allocated stack based on the generated id */
288-
andl 0x0000FFFF, %ebx
289-
movl %ebx, %esi
290-
movl (ap_cpu_init_stacks - apic_ap_start)(%ebp, %ebx, 4), %esp
291-
285+
movl (ap_cpu_init_stacks - apic_ap_start)(%ebp, %eax, 4), %esp
286+
292287
/* check if we support NX and enable it if we do */
293288
movl $0x80000001, %eax
294289
cpuid
@@ -319,8 +314,8 @@ apic_ap_start32:
319314
lgdt (ap_cpu_gdtr_initial2 - apic_ap_start + 0xc0008000)
320315

321316
/* jump above 3GB into our identity mapped area now */
322-
ljmp $8, $(1f - apic_ap_start + 0xc0008000)
323-
1:
317+
ljmp $8, $(apic_ap_start32_2 - apic_ap_start + 0xc0008000)
318+
apic_ap_start32_2:
324319
/* flush the TLB */
325320
movl %cr3, %eax
326321
movl %eax, %cr3
@@ -338,13 +333,20 @@ apic_ap_start32:
338333
movl %eax, %cr0
339334
movl (ap_cpu_init_cr4 - apic_ap_start)(%ebp), %eax
340335
movl %eax, %cr4
341-
336+
337+
/* push the Processor pointer this CPU is going to use */
338+
movl (ap_cpu_init_processor_info_array - apic_ap_start)(%ebp), %eax
339+
addl $0xc0000000, %eax
340+
movl 0(%eax, %esi, 4), %eax
341+
push %eax
342+
343+
/* push the cpu id, 0 representing the bsp and call into c++ */
344+
incl %esi
345+
push %esi
346+
342347
xor %ebp, %ebp
343348
cld
344349

345-
/* push the arbitrary cpu id, 0 representing the bsp and call into c++ */
346-
inc %esi
347-
push %esi
348350
/* We are in identity mapped P0x8000 and the BSP will unload this code
349351
once all APs are initialized, so call init_ap but return to our
350352
infinite loop */
@@ -356,7 +358,7 @@ apic_ap_start32:
356358
apic_ap_start_size:
357359
.2byte end_apic_ap_start - apic_ap_start
358360
ap_cpu_id:
359-
.2byte 0x0
361+
.4byte 0x0
360362
ap_cpu_gdt:
361363
/* null */
362364
.8byte 0x0
@@ -388,6 +390,9 @@ ap_cpu_init_cr3:
388390
.global ap_cpu_init_cr4
389391
ap_cpu_init_cr4:
390392
.4byte 0x0 /* will be set at runtime */
393+
.global ap_cpu_init_processor_info_array
394+
ap_cpu_init_processor_info_array:
395+
.4byte 0x0 /* will be set at runtime */
391396
.global ap_cpu_init_stacks
392397
ap_cpu_init_stacks:
393398
/* array of allocated stack pointers */

0 commit comments

Comments
 (0)