furix
cute os
include/arch/x86_64/gdt.h
1562 bytes raw
1#ifndef FURIX_ARCH_X86_64_GDT_H
2#define FURIX_ARCH_X86_64_GDT_H
3
4#include <types.h>
5
6/* long-mode segment selectors, in the kernel GDT */
7#define SEGSEL_KERNEL_CODE 0x08
8#define SEGSEL_KERNEL_DATA 0x10
9#define SEGSEL_TSS 0x18
10
11/*
12 * selector of the TSS descriptor in the AP trampoline's flat GDT. that table is
13 * built in ap_boot.S and is separate from the kernel GDT above, so it has its
14 * own numbering; the AP must select the descriptor from ITS table.
15 */
16#define AP_SEGSEL_TSS 0x20
17
18/* IST indices, 1-based; 0 means "use the current stack" */
19#define IST_DOUBLE_FAULT 1
20#define IST_NMI 2
21
22#define IST_STACK_SIZE 8192
23
24/*
25 * the 64-bit TSS. declared here because both the kernel GDT and the per-AP
26 * TSSes are built from it, and ap_boot.S's AP IDT references the IST slots by
27 * index; a layout change here has to move in lockstep with both.
28 */
29struct tss {
30 uint32_t reserved0;
31 uint32_t reserved0_pad;
32 uint64_t rsp[3];
33 uint64_t reserved1;
34 uint64_t ist[7];
35 uint16_t reserved2;
36 uint16_t iomap_base;
37} __attribute__((packed));
38
39/* install the kernel GDT and reload the segment registers, including CS */
40void gdt_init(void);
41
42/*
43 * build the TSS for the AP in `slot` and return its base address, for the BSP
44 * to patch into the AP's copy of the trampoline GDT.
45 *
46 * each AP needs its own because the TSS holds the IST stack POINTERS: sharing
47 * one would put two processors on the same IST stack, which is exactly the
48 * corruption the IST exists to prevent.
49 */
50uint64_t gdt_ap_tss_base(uint8_t slot);
51
52#endif /* FURIX_ARCH_X86_64_GDT_H */