]> 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 dcb196d91112e7c82a502d23e36d253b056d0d2b..1ad85a882d8c58ecf6d4f7792e36f2606aa2052a 100644 (file)
@@ -15,4 +15,6 @@ void hal_cpu_idle(void);
 
 uint64_t hal_cpu_read_timestamp(void);
 
+void hal_cpu_set_tls(uintptr_t base);
+
 #endif
index 1adc72ecf093d0da8c6a4fdd5c7a402dace14f33..d863996c38afa79967f5035f91578bc253de1b36 100644 (file)
@@ -54,6 +54,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) {
@@ -85,4 +94,8 @@ uint64_t hal_cpu_read_timestamp(void) {
     return 0;
 }
 
+void hal_cpu_set_tls(uintptr_t base) {
+    (void)base;
+}
+
 #endif
index e6cc36e43287bad0048d57126b2d63fb51252f31..7a5d326d5e29ed86b361efec1eb40e52e61bd6f0 100644 (file)
@@ -434,18 +434,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);
 }