Indentation fix, cleanup.
[AROS.git] / arch / x86_64-pc / kernel / kernel_startup.c
blobe8331ab16120c26ddfb32fa0c1583d5033a10f44
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/multiboot.h>
7 #include <asm/cpu.h>
8 #include <asm/io.h>
9 #include <aros/symbolsets.h>
10 #include <exec/lists.h>
11 #include <exec/memory.h>
12 #include <exec/resident.h>
13 #include <utility/tagitem.h>
14 #include <proto/arossupport.h>
15 #include <proto/exec.h>
17 #include <bootconsole.h>
18 #include <inttypes.h>
19 #include <string.h>
21 #include "boot_utils.h"
22 #include "kernel_base.h"
23 #include "kernel_bootmem.h"
24 #include "kernel_debug.h"
25 #include "kernel_intern.h"
26 #include "kernel_mmap.h"
27 #include "kernel_romtags.h"
28 #include "apic.h"
29 #include "smp.h"
30 #include "tls.h"
32 #define D(x) x
33 #define DSTACK(x)
35 /* Common IBM PC memory layout */
36 static const struct MemRegion PC_Memory[] =
39 * Give low memory a bit lower priority. This will help us to locate its MemHeader (the last one in the list).
40 * We explicitly need low memory for SMP bootstrap.
42 {0x000000000, 0x000100000, "Low memory" , -6, MEMF_PUBLIC|MEMF_LOCAL|MEMF_KICK|MEMF_CHIP|MEMF_31BIT|MEMF_24BITDMA},
43 {0x000100000, 0x001000000, "ISA DMA memory", -5, MEMF_PUBLIC|MEMF_LOCAL|MEMF_KICK|MEMF_CHIP|MEMF_31BIT|MEMF_24BITDMA},
45 * EXPERIMENTAL:
46 * 1. Some (or all?) 64-bit machines expose RAM at addresses up to 0xD0000000 (giving 3.5 GB total). All MMIO
47 * sits beyond this border. We intentionally specify 4GB as limit, just in case if some machine exhibits
48 * even more RAM in this space. We want all the RAM to be usable.
49 * 2. We have MEMF_31BIT originating from MorphOS. But here we interpret it as "32-bit memory". I guess
50 * it originated from the assumption that MMIO starts at 0x80000000 (which is true at least for PegasosPPC).
51 * So, is it okay to assume actually 32-bit memory for MEMF_31BIT? Are there anything which really imposes
52 * 31-bit limit? AllocEntry() issue doesn't count...
54 {0x001000000, 0x080000000, "31-bit memory" , 0, MEMF_PUBLIC|MEMF_LOCAL|MEMF_KICK|MEMF_CHIP|MEMF_31BIT },
55 {0x080000000, -1, "High memory" , 10, MEMF_PUBLIC|MEMF_LOCAL|MEMF_KICK|MEMF_FAST },
56 {0 , 0 , NULL , 0, 0 }
59 static ULONG allocator = ALLOCATOR_TLSF;
62 * Boot-time global variables.
63 * __KernBootPrivate needs to survive accross warm reboots, so it's put into .data.
64 * SysBase is intentionally put into .rodata. This way we prevent it from being modified.
66 __attribute__((section(".data"))) struct KernBootPrivate *__KernBootPrivate = NULL;
67 __attribute__((section(".data"))) IPTR kick_highest = 0;
68 __attribute__((section(".rodata"))) struct ExecBase *SysBase = NULL;
70 static void boot_start(struct TagItem *msg);
71 static char boot_stack[];
74 * This is where our kernel started.
75 * First we clear BSS section, then switch stack pointer to our temporary stack
76 * (which is itself located in BSS). While we are here, the stack is actually
77 * located inside our bootstrap, and it's safe to use it a little bit.
79 IPTR __startup start64(struct TagItem *msg, ULONG magic)
81 /* Anti-command-line-run protector */
82 if (magic == AROS_BOOT_MAGIC)
84 /* Run the kickstart from boot_start() routine. */
85 core_Kick(msg, boot_start);
88 return -1;
92 * This code is executed only once, after the kickstart is loaded by bootstrap.
93 * Its main job is to initialize early debugging console ASAP in order to be able
94 * to see what happens. This will deal with both serial and on-screen console.
96 * Console mirror is placed at the end of bootstrap's protected area. We must not
97 * overwrite it because it contains boot-time GDT, taglist, and some other structures.
99 * Default address is bootstrap start + 4KB, just in case.
101 static void boot_start(struct TagItem *msg)
103 fb_Mirror = (void *)LibGetTagData(KRN_ProtAreaEnd, 0x101000, msg);
104 con_InitTagList(msg);
106 bug("AROS64 - The AROS Research OS, 64-bit version. Compiled %s\n", __DATE__);
107 D(bug("[Kernel] boot_start: Jumped into kernel.resource @ %p [stub @ %p].\n", boot_start, start64));
109 kernel_cstart(msg);
113 * This routine actually launches the kickstart. It's called either upon first start or upon warm reboot.
114 * The only assumption is that stack is outside .bss . For both cases this is true:
115 * 1. First boot - the stack is located inside the bootstrap.
116 * 2. Warm reboot - the stack is located in supervisor area (__KernBootPrivate->SystemStack).
118 void core_Kick(struct TagItem *msg, void *target)
120 const struct TagItem *bss = LibFindTagItem(KRN_KernelBss, msg);
122 /* First clear .bss */
123 if (bss)
124 __clear_bss((const struct KernelBSS *)bss->ti_Data);
127 * ... then switch to initial stack and jump to target address.
128 * We set rbp to 0 and use call here in order to get correct stack traces
129 * if the boot task crashes. Otherwise backtrace goes beyond this location
130 * into memory areas with undefined contents.
132 asm volatile("movq %1, %%rsp\n\t"
133 "movq $0, %%rbp\n\t"
134 "call *%2\n"::"D"(msg), "r"(boot_stack + STACK_SIZE), "r"(target));
138 * This is the main entry point.
139 * We run from here both at first boot and upon reboot.
141 void kernel_cstart(const struct TagItem *start_msg)
143 struct MinList memList;
144 struct TagItem *msg = (struct TagItem *)start_msg;
145 struct MemHeader *mh, *mh2;
146 struct mb_mmap *mmap = NULL;
147 IPTR mmap_len = 0;
148 IPTR addr = 0;
149 IPTR klo = 0;
150 IPTR memtop = 0;
151 struct TagItem *tag;
152 UBYTE _APICID;
153 UWORD *ranges[] = {NULL, NULL, (UWORD *)-1};
154 /* Enable fxsave/fxrstor */
155 wrcr(cr4, rdcr(cr4) | _CR4_OSFXSR | _CR4_OSXMMEXCPT);
157 D(bug("[Kernel] Boot data: 0x%p\n", __KernBootPrivate));
158 DSTACK(bug("[Kernel] Boot stack: 0x%p - 0x%p\n", boot_stack, boot_stack + STACK_SIZE));
160 if (__KernBootPrivate == NULL)
162 /* This is our first start. */
163 struct vbe_mode *vmode = NULL;
164 char *cmdline = NULL;
165 IPTR khi;
167 /* We need highest KS address and memory map to begin the work */
168 khi = LibGetTagData(KRN_KernelHighest, 0, msg);
169 mmap = (struct mb_mmap *)LibGetTagData(KRN_MMAPAddress, 0, msg);
170 mmap_len = LibGetTagData(KRN_MMAPLength, 0, msg);
172 if ((!khi) || (!mmap) || (!mmap_len))
174 krnPanic(NULL, "Incomplete information from the bootstrap\n"
175 "\n"
176 "Kickstart top: 0x%p\n"
177 "Memory map: address 0x%p, length %lu\n", khi, mmap, mmap, mmap_len);
181 * Our boot taglist is located just somewhere in memory. Additionally, it's very fragmented
182 * (its linked data, like VBE information, were also placed just somewhere, by GRUB.
183 * Now we need some memory to gather these things together. This memory will be preserved
184 * accross warm restarts.
185 * We know the bootstrap has reserved some space right beyond the kickstart. We get our highest
186 * address, and use memory map to locate topmost address of this area.
188 khi = AROS_ROUNDUP2(khi + 1, sizeof(APTR));
189 mmap = mmap_FindRegion(khi, mmap, mmap_len);
191 if (!mmap)
193 krnPanic(NULL, "Inconsistent memory map or kickstart placement\n"
194 "Kickstart region not found");
197 if (mmap->type != MMAP_TYPE_RAM)
199 krnPanic(NULL, "Inconsistent memory map or kickstart placement\n"
200 "Reserved memory overwritten\n"
201 "Region 0x%p - 0x%p type %d\n"
202 "Kickstart top 0x%p", mmap->addr, mmap->addr + mmap->len - 1, mmap->type, khi);
205 /* Initialize boot-time memory allocator */
206 BootMemPtr = (void *)khi;
207 BootMemLimit = (void *)mmap->addr + mmap->len;
209 D(bug("[Kernel] Bootinfo storage 0x%p - 0x%p\n", BootMemPtr, BootMemLimit));
212 * Our boot taglist is placed by the bootstrap just somewhere in memory.
213 * The first thing is to move it into some safe place.
216 /* This will relocate the taglist itself */
217 RelocateBootMsg(msg);
220 * Now relocate linked data.
221 * Here we actually process only tags we know about and expect to get.
222 * For example, we are not going to receive KRN_HostInterface or KRN_OpenfirmwareTree.
224 msg = BootMsg;
225 while ((tag = LibNextTagItem(&msg)))
227 switch (tag->ti_Tag)
229 case KRN_KernelBss:
230 RelocateBSSData(tag);
231 break;
233 case KRN_MMAPAddress:
234 RelocateTagData(tag, mmap_len);
235 break;
237 case KRN_VBEModeInfo:
238 RelocateTagData(tag, sizeof(struct vbe_mode));
239 vmode = (struct vbe_mode *)tag->ti_Data;
240 break;
242 case KRN_VBEControllerInfo:
243 RelocateTagData(tag, sizeof(struct vbe_controller));
244 break;
246 case KRN_CmdLine:
247 RelocateStringData(tag);
248 cmdline = (char *)tag->ti_Data;
249 break;
251 case KRN_BootLoader:
252 RelocateStringData(tag);
253 break;
257 /* Now allocate KernBootPrivate */
258 __KernBootPrivate = krnAllocBootMem(sizeof(struct KernBootPrivate));
260 if (cmdline && vmode && vmode->phys_base && strstr(cmdline, "vesahack"))
262 bug("[Kernel] VESA debugging hack activated\n");
265 * VESA hack.
266 * It divides screen height by 2 and increments framebuffer pointer.
267 * This allows VESA driver to use only upper half of the screen, while
268 * lower half will still be used for debug output.
270 vmode->y_resolution >>= 1;
272 __KernBootPrivate->debug_y_resolution = vmode->y_resolution;
273 __KernBootPrivate->debug_framebuffer = (void *)(unsigned long)vmode->phys_base + vmode->y_resolution * vmode->bytes_per_scanline;
276 if (cmdline && strstr(cmdline, "notlsf"))
277 allocator = ALLOCATOR_STD;
280 /* Prepare GDT */
281 core_SetupGDT(__KernBootPrivate);
283 if (!__KernBootPrivate->SystemStack)
286 * Allocate our supervisor stack from boot-time memory.
287 * It will be protected from user's intervention.
288 * Allocate actually three stacks: panic, supervisor, ring1.
289 * Note that we do the actual allocation only once. The region is kept
290 * in __KernBootPrivate which survives warm reboots.
292 __KernBootPrivate->SystemStack = (IPTR)krnAllocBootMem(STACK_SIZE * 3);
294 DSTACK(bug("[Kernel] Allocated supervisor stack 0x%p - 0x%p\n",
295 __KernBootPrivate->SystemStack, __KernBootPrivate->SystemStack + STACK_SIZE * 3));
298 /* We are x86-64, and we know we always have APIC. */
299 __KernBootPrivate->_APICBase = core_APIC_GetBase();
300 _APICID = core_APIC_GetID(__KernBootPrivate->_APICBase);
301 D(bug("[Kernel] kernel_cstart: launching on BSP APIC ID %d, base @ %p\n", _APICID, __KernBootPrivate->_APICBase));
303 memtop = mmap_LargestAddress(mmap, mmap_len);
304 /* Set TSS, GDT, LDT and MMU up */
305 core_CPUSetup(_APICID, __KernBootPrivate->SystemStack);
306 core_SetupIDT(__KernBootPrivate);
307 core_SetupMMU(__KernBootPrivate, memtop);
310 * Here we ended all boot-time allocations.
311 * We won't do them again, for example on warm reboot. All our areas are stored in struct KernBootPrivate.
312 * We are going to make this area read-only and reset-proof.
314 if (!kick_highest)
316 D(bug("[Kernel] Boot-time setup complete\n"));
317 kick_highest = AROS_ROUNDUP2((IPTR)BootMemPtr, PAGE_SIZE);
320 D(bug("[Kernel] End of kickstart area 0x%p\n", kick_highest));
323 * Obtain the needed data from the boot taglist.
324 * We need to do this even on first boot, because the taglist and its data
325 * have been moved to the permanent storage.
327 msg = BootMsg;
328 while ((tag = LibNextTagItem(&msg)))
330 switch (tag->ti_Tag)
332 case KRN_KernelBase:
334 * KRN_KernelBase is actually a border between read-only
335 * (code) and read-write (data) sections of the kickstart.
336 * read-write section goes to lower addresses from this one,
337 * so we align it upwards in order not to make part of RW data
338 * read-only.
340 addr = AROS_ROUNDUP2(tag->ti_Data, PAGE_SIZE);
341 break;
343 case KRN_KernelLowest:
344 klo = AROS_ROUNDDOWN2(tag->ti_Data, PAGE_SIZE);
345 break;
347 case KRN_MMAPAddress:
348 mmap = (struct mb_mmap *)tag->ti_Data;
349 break;
351 case KRN_MMAPLength:
352 mmap_len = tag->ti_Data;
353 break;
357 /* Sanity check */
358 if ((!klo) || (!addr))
360 krnPanic(NULL, "Incomplete information from the bootstrap\n"
361 "\n"
362 "Kickstart lowest 0x%p, base 0x%p\n", klo, addr);
366 * Explore memory map and create MemHeaders.
367 * We reserve one page (PAGE_SIZE) at zero address. We will protect it.
369 NEWLIST(&memList);
370 mmap_InitMemory(mmap, mmap_len, &memList, klo, kick_highest, PAGE_SIZE, PC_Memory, allocator);
372 D(bug("[Kernel] kernel_cstart: Booting exec.library...\n"));
375 * mmap_InitMemory() adds MemHeaders to the list in the order they were created.
376 * I. e. highest addresses are added last.
377 * Take highest region in order to create SysBase in it.
379 mh = (struct MemHeader *)REMTAIL(&memList);
380 D(bug("[Kernel] Initial MemHeader: 0x%p - 0x%p (%s)\n", mh->mh_Lower, mh->mh_Upper, mh->mh_Node.ln_Name));
382 if (SysBase)
384 D(bug("[Kernel] Got old SysBase 0x%p...\n", SysBase));
386 * Validate existing SysBase pointer.
387 * Here we check that if refers to a valid existing memory region.
388 * Checksums etc are checked in arch-independent code in exec.library.
389 * It's enough to use only size of public part. Anyway, SysBase will be
390 * reallocated by PrepareExecBase(), it will just keep over some data from
391 * public part (KickMemPtr, KickTagPtr and capture vectors).
393 if (!mmap_ValidateRegion((unsigned long)SysBase, sizeof(struct ExecBase), mmap, mmap_len))
395 D(bug("[Kernel] ... invalidated\n"));
396 SysBase = NULL;
400 /* This handles failures itself */
401 ranges[0] = (UWORD *)klo;
402 ranges[1] = (UWORD *)kick_highest;
403 krnPrepareExecBase(ranges, mh, BootMsg);
405 krnCreateROMHeader("Kickstart ROM", (APTR)klo, (APTR)kick_highest);
408 * Now we have working exec.library memory allocator.
409 * Move console mirror buffer away from unused memory.
410 * WARNING!!! Do not report anything in the debug log before this is done. Remember that sequental
411 * AllocMem()s return sequental blocks! And right beyond our allocated area there will be MemChunk.
412 * Between krnPrepareExecBase() and this AllocMem() upon warm reboot console mirror buffer is set
413 * to an old value right above ExecBase. During krnPrepareExecBase() a MemChunk is built there,
414 * which can be overwritten by bootconsole, especially if the output scrolls.
416 if (scr_Type == SCR_GFX)
418 char *mirror = AllocMem(scr_Width * scr_Height, MEMF_PUBLIC);
420 fb_SetMirror(mirror);
423 D(bug("[Kernel] Created SysBase at 0x%p (pointer at 0x%p), MemHeader 0x%p\n", SysBase, &SysBase, mh));
425 /* Block all user's access to zero page */
426 core_ProtKernelArea(0, PAGE_SIZE, 1, 0, 0);
428 /* Store important private data */
429 TLS_SET(SysBase, SysBase);
431 /* Provide information about our supevisor stack. Useful at least for diagnostics. */
432 SysBase->SysStkLower = (APTR)__KernBootPrivate->SystemStack;
433 SysBase->SysStkUpper = (APTR)__KernBootPrivate->SystemStack + STACK_SIZE * 3;
436 * Make kickstart code area read-only.
437 * We do it only after ExecBase creation because SysBase pointer is put
438 * into .rodata. This way we prevent it from ocassional modification by buggy software.
440 core_ProtKernelArea(addr, kick_highest - addr, 1, 0, 1);
442 /* Transfer the rest of memory list into SysBase */
443 D(bug("[Kernel] Transferring memory list into SysBase...\n"));
444 for (mh = (struct MemHeader *)memList.mlh_Head; mh->mh_Node.ln_Succ; mh = mh2)
446 mh2 = (struct MemHeader *)mh->mh_Node.ln_Succ;
448 D(bug("[Kernel] * 0x%p - 0x%p (%s)\n", mh->mh_Lower, mh->mh_Upper, mh->mh_Node.ln_Name));
449 Enqueue(&SysBase->MemList, &mh->mh_Node);
453 * RTF_SINGLETASK residents are called with supervisor privilege level.
454 * Original AmigaOS(tm) does the same, some Amiga hardware expansion ROM
455 * rely on it. Here we continue the tradition, because it's useful for
456 * acpica.library (which needs to look for RSDP in the first 1M)
458 InitCode(RTF_SINGLETASK, 0);
461 * After InitCode(RTF_SINGLETASK) we may have acpica.library
462 * Now we can use ACPI information in order to set up advanced things (SMP, APIC, etc).
463 * Interrupts are still disabled and we are still supervisor.
466 PlatformPostInit();
468 /* Drop privileges down to user mode before calling RTF_COLDSTART */
469 D(bug("[Kernel] Leaving supervisor mode\n"));
470 asm volatile (
471 "mov %[user_ds],%%ds\n\t" // Load DS and ES
472 "mov %[user_ds],%%es\n\t"
473 "mov %%rsp,%%r12\n\t"
474 "pushq %[ds]\n\t" // SS
475 "pushq %%r12\n\t" // rSP
476 "pushq $0x3002\n\t" // rFLAGS
477 "pushq %[cs]\n\t" // CS
478 "pushq $1f\n\t"
479 "iretq\n 1:"
480 ::[user_ds]"r"(USER_DS),[ds]"i"(USER_DS),[cs]"i"(USER_CS):"r12");
483 * We are fully done. Run exec.library and the rest.
484 * exec.library will be the first resident to run. It will enable interrupts and multitasking for us.
486 InitCode(RTF_COLDSTART, 0);
488 /* The above must not return */
489 krnPanic(KernelBase, "System Boot Failed!");
492 /* Small delay routine used by exec_cinit initializer */
493 asm("\ndelay:\t.short 0x00eb\n\tretq");
495 /* Our boot-time stack */
496 static char boot_stack[STACK_SIZE] __attribute__((aligned(16)));
498 struct gdt_64bit
500 struct segment_desc seg0; /* seg 0x00 */
501 struct segment_desc super_cs; /* seg 0x08 */
502 struct segment_desc super_ds; /* seg 0x10 */
503 struct segment_desc user_cs32; /* seg 0x18 */
504 struct segment_desc user_ds; /* seg 0x20 */
505 struct segment_desc user_cs; /* seg 0x28 */
506 struct segment_desc gs; /* seg 0x30 */
507 struct segment_desc ldt; /* seg 0x38 */
508 struct
510 struct segment_desc tss_low; /* seg 0x40... */
511 struct segment_ext tss_high;
512 } tss[16];
515 void core_SetupGDT(struct KernBootPrivate *__KernBootPrivate)
517 struct gdt_64bit *GDT;
518 struct tss_64bit *TSS;
519 intptr_t tls_ptr;
520 int i;
522 D(bug("[Kernel] core_SetupGDT(0x%p)\n", __KernBootPrivate));
524 if (!__KernBootPrivate->GDT)
526 __KernBootPrivate->system_tls = krnAllocBootMem(sizeof(tls_t));
527 __KernBootPrivate->GDT = krnAllocBootMemAligned(sizeof(struct gdt_64bit), 128);
528 __KernBootPrivate->TSS = krnAllocBootMemAligned(sizeof(struct tss_64bit) * 16, 128);
530 D(bug("[Kernel] Allocated GDT 0x%p, TLS 0x%p\n", __KernBootPrivate->GDT, __KernBootPrivate->system_tls));
533 GDT = __KernBootPrivate->GDT;
534 TSS = __KernBootPrivate->TSS;
536 /* Supervisor segments */
537 GDT->super_cs.type=0x1a; /* code segment */
538 GDT->super_cs.dpl=0; /* supervisor level */
539 GDT->super_cs.p=1; /* present */
540 GDT->super_cs.l=1; /* long (64-bit) one */
541 GDT->super_cs.d=0; /* must be zero */
542 GDT->super_cs.limit_low=0xffff;
543 GDT->super_cs.limit_high=0xf;
544 GDT->super_cs.g=1;
546 GDT->super_ds.type=0x12; /* data segment */
547 GDT->super_ds.dpl=0; /* supervisor level */
548 GDT->super_ds.p=1; /* present */
549 GDT->super_ds.limit_low=0xffff;
550 GDT->super_ds.limit_high=0xf;
551 GDT->super_ds.g=1;
552 GDT->super_ds.d=1;
554 /* User mode segments */
555 GDT->user_cs.type=0x1a; /* code segment */
556 GDT->user_cs.dpl=3; /* User level */
557 GDT->user_cs.p=1; /* present */
558 GDT->user_cs.l=1; /* long mode */
559 GDT->user_cs.d=0; /* must be zero */
560 GDT->user_cs.limit_low=0xffff;
561 GDT->user_cs.limit_high=0xf;
562 GDT->user_cs.g=1;
564 GDT->user_cs32.type=0x1a; /* code segment for legacy 32-bit code. NOT USED YET! */
565 GDT->user_cs32.dpl=3; /* user level */
566 GDT->user_cs32.p=1; /* present */
567 GDT->user_cs32.l=0; /* 32-bit mode */
568 GDT->user_cs32.d=1; /* 32-bit code */
569 GDT->user_cs32.limit_low=0xffff;
570 GDT->user_cs32.limit_high=0xf;
571 GDT->user_cs32.g=1;
573 GDT->user_ds.type=0x12; /* data segment */
574 GDT->user_ds.dpl=3; /* user level */
575 GDT->user_ds.p=1; /* present */
576 GDT->user_ds.limit_low=0xffff;
577 GDT->user_ds.limit_high=0xf;
578 GDT->user_ds.g=1;
579 GDT->user_ds.d=1;
581 for (i=0; i < 16; i++)
583 const unsigned long tss_limit = sizeof(struct tss_64bit) * 16 - 1;
585 /* Task State Segment */
586 GDT->tss[i].tss_low.type = 0x09; /* 64-bit TSS */
587 GDT->tss[i].tss_low.limit_low = tss_limit;
588 GDT->tss[i].tss_low.base_low = ((unsigned long)&TSS[i]) & 0xffff;
589 GDT->tss[i].tss_low.base_mid = (((unsigned long)&TSS[i]) >> 16) & 0xff;
590 GDT->tss[i].tss_low.dpl = 3; /* User mode task */
591 GDT->tss[i].tss_low.p = 1; /* present */
592 GDT->tss[i].tss_low.limit_high = (tss_limit >> 16) & 0x0f;
593 GDT->tss[i].tss_low.base_high = (((unsigned long)&TSS[i]) >> 24) & 0xff;
594 GDT->tss[i].tss_high.base_ext = 0; /* is within 4GB :-D */
597 tls_ptr = (intptr_t)__KernBootPrivate->system_tls;
599 GDT->gs.type=0x12; /* data segment */
600 GDT->gs.dpl=3; /* user level */
601 GDT->gs.p=1; /* present */
602 GDT->gs.base_low = tls_ptr & 0xffff;
603 GDT->gs.base_mid = (tls_ptr >> 16) & 0xff;
604 GDT->gs.base_high = (tls_ptr >> 24) & 0xff;
605 GDT->gs.g=1;
606 GDT->gs.d=1;
609 void core_CPUSetup(UBYTE _APICID, IPTR SystemStack)
611 struct segment_selector GDT_sel;
612 struct tss_64bit *TSS = __KernBootPrivate->TSS;
614 D(bug("[Kernel] core_CPUSetup(%d, 0x%p)\n", _APICID, SystemStack));
617 * At the moment two of three stacks are reserved. IST is not used (indexes == 0 in interrupt gates)
618 * and ring 1 is not used either. However, the space pointed to by IST is used as a temporary stack
619 * for warm restart routine.
622 TSS[_APICID].ist1 = SystemStack + STACK_SIZE - 16; /* Interrupt stack entry 1 (failsafe) */
623 TSS[_APICID].rsp0 = SystemStack + STACK_SIZE * 2 - 16; /* Ring 0 (Supervisor) */
624 TSS[_APICID].rsp1 = SystemStack + STACK_SIZE * 3 - 16; /* Ring 1 (reserved) */
626 D(bug("[Kernel] core_CPUSetup[%d]: Reloading the GDT and Task Register\n", _APICID));
628 GDT_sel.size = sizeof(struct gdt_64bit) - 1;
629 GDT_sel.base = (uint64_t)__KernBootPrivate->GDT;
630 asm volatile ("lgdt %0"::"m"(GDT_sel));
631 asm volatile ("ltr %w0"::"r"(TASK_SEG + (_APICID << 4)));
632 asm volatile ("mov %0,%%gs"::"a"(USER_GS));