]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
refactor: extract x86 GDT/GS TLS setup from scheduler to HAL layer
authorTulio A M Mendes <[email protected]>
Thu, 12 Feb 2026 06:42:19 +0000 (03:42 -0300)
committerTulio A M Mendes <[email protected]>
Fri, 13 Feb 2026 02:44:55 +0000 (23:44 -0300)
Added hal_cpu_set_tls(base) to the HAL CPU API with x86 implementation
(GDT entry 22 + GS segment load) and a no-op fallback for other arches.

kernel/scheduler.c no longer contains x86 inline assembly for TLS —
the #if defined(__i386__) block is replaced by a single HAL call.

include/hal/cpu.h
src/hal/x86/cpu.c
src/kernel/scheduler.c

index 1a1413acfbd67e1181a1adb90b3130162cd8504b..31ceb7f44f7a61cf954668fcdfe6c9fd6aeff0b4 100644 (file)
@@ -24,4 +24,6 @@ void hal_cpu_idle(void);
 
 uint64_t hal_cpu_read_timestamp(void);
 
+void hal_cpu_set_tls(uintptr_t base);
+
 #endif
index 6e801c80287720bf0e8c51e28e6fa290a99dbd61..5055058ad69a9a3e54c8cc25bd241406f00ca7c9 100644 (file)
@@ -63,6 +63,15 @@ uint64_t hal_cpu_read_timestamp(void) {
     return ((uint64_t)hi << 32) | lo;
 }
 
+void hal_cpu_set_tls(uintptr_t base) {
+    /* GDT entry 22: user TLS segment (ring 3, data RW) */
+    gdt_set_gate_ext(22, (uint32_t)base, 0xFFFFF, 0xF2, 0xCF);
+    __asm__ volatile(
+        "mov $0xB3, %%ax\n"
+        "mov %%ax, %%gs\n" : : : "ax"
+    ); /* selector = 22*8 | RPL=3 = 0xB3 */
+}
+
 #else
 
 uintptr_t hal_cpu_get_stack_pointer(void) {
@@ -94,4 +103,8 @@ uint64_t hal_cpu_read_timestamp(void) {
     return 0;
 }
 
+void hal_cpu_set_tls(uintptr_t base) {
+    (void)base;
+}
+
 #endif
index 141f9930bbdff44eab9ef3e6bd1af6def4efee20..8f4c0320208ad17032babdb8b56c0dd6dddd586d 100644 (file)
@@ -443,18 +443,9 @@ static void clone_child_trampoline(void) {
     }
 
     /* Load user TLS into GS if set */
-#if defined(__i386__)
     if (current_process->tls_base) {
-        extern void gdt_set_gate_ext(int num, uint32_t base, uint32_t limit,
-                                      uint8_t access, uint8_t gran);
-        /* Use GDT entry 22 as the user TLS segment (ring 3, data RW) */
-        gdt_set_gate_ext(22, (uint32_t)current_process->tls_base, 0xFFFFF, 0xF2, 0xCF);
-        __asm__ volatile(
-            "mov $0xB3, %%ax\n"
-            "mov %%ax, %%gs\n" : : : "ax"
-        ); /* selector = 22*8 | RPL=3 = 0xB3 */
-    }
-#endif
+        hal_cpu_set_tls(current_process->tls_base);
+    }
 
     hal_usermode_enter_regs(&current_process->user_regs);
 }