From 831580798cea72f99be1f98ddd18bc3f68e0acae Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 17 Apr 2026 12:40:13 -0400 Subject: [PATCH] sp1/amd64: cpu: Add GDT sources Signed-off-by: Ian Moffett --- usr/src/sp1/amd64/cpu/locore.S | 81 ++++++++++++++++++++++++++++++++++ usr/src/sp1/head/amd64/gdt.h | 27 ++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 usr/src/sp1/head/amd64/gdt.h diff --git a/usr/src/sp1/amd64/cpu/locore.S b/usr/src/sp1/amd64/cpu/locore.S index e64ffe1..357ef31 100644 --- a/usr/src/sp1/amd64/cpu/locore.S +++ b/usr/src/sp1/amd64/cpu/locore.S @@ -37,9 +37,90 @@ md_cpu_init: or $1<<11, %eax // Set NXE wrmsr // Write it back + lea g_GDTR(%rip), %rdi + call md_gdt_load + pop %rbx pop %r15 pop %r14 pop %r13 pop %r12 retq + + .text + .globl md_gdt_load +md_gdt_load: + push %r12 + push %r13 + push %r14 + push %r15 + push %rbx + push %rbp + + lgdt (%rdi) /* Load our own GDT */ + push $0x08 /* Kernel CS */ + lea 1f(%rip), %rbx /* Drop point */ + push %rbx /* RIP */ + lretq /* Flush prefetch + reload CS */ +1: mov $0x10, %ax /* Kernel DS */ + mov %ax, %ds + mov %ax, %es + mov %ax, %ss + mov %ax, %fs + xor %ax, %ax + mov %ax, %gs + + pop %rbp + pop %rbx + pop %r15 + pop %r14 + pop %r13 + pop %r12 + retq + + .section .data + .globl g_GDT +g_GDT: +.NULL: + .word 0x0000 + .word 0x0000 + .byte 0x00 + .byte 0b00000000 + .byte 0b00000000 + .byte 0x00 +.CODE: + .word 0x0000 + .word 0x0000 + .byte 0x00 + .byte 0b10011010 + .byte 0b00100000 + .byte 0x00 +.DATA: + .word 0x0000 + .word 0x0000 + .byte 0x00 + .byte 0b10010010 + .byte 0b00000000 + .byte 0x00 +.UCODE: + .word 0x0000 + .word 0x0000 + .byte 0x00 + .byte 0b11111010 + .byte 0b10101111 + .byte 0x00 +.UDATA: + .word 0x0000 + .word 0x0000 + .byte 0x00 + .byte 0b11110010 + .byte 0b00000000 + .byte 0x00 +.TSS: + .quad 0x00 + .quad 0x00 + + .globl g_GDTR +g_GDTR: + .word g_GDTR - g_GDT + .quad g_GDT diff --git a/usr/src/sp1/head/amd64/gdt.h b/usr/src/sp1/head/amd64/gdt.h new file mode 100644 index 0000000..829dac5 --- /dev/null +++ b/usr/src/sp1/head/amd64/gdt.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2026, Mirocom Laboratories + * All rights reserved. + * + * The following sources are CONFIDENTIAL and PROPRIETARY + * property of Mirocom Laboratories. Unauthorized copying, + * use, distribution or modification of this file, in whole + * and in part, is strictly prohibited without the prior written + * consent from Mirocom Laboratories. + */ + +#ifndef _MACHINE_GDT_H_ +#define _MACHINE_GDT_H_ 1 + +/* Kernel code/data */ +#define GDT_KCODE 0x08 +#define GDT_KDATA 0x10 + +/* User code/data */ +#define GDT_UCODE 0x18 +#define GDT_UDATA 0x20 + +/* Task state segment */ +#define GDT_TSS 0x28 +#define GDT_TSS_INDEX 0x05 + +#endif /* !_MACHINE_GDT_H_ */