From ed2febb28990982624e5d41c5bccd5bf8f22af08 Mon Sep 17 00:00:00 2001
From: 4lDO2 <4lDO2@protonmail.com>
Date: Tue, 11 Jul 2023 16:18:20 +0200
Subject: [PATCH] Disable thread-local storage entirely.

---
 linkers/i686.ld                    | 13 -------------
 linkers/x86_64.ld                  | 11 -----------
 src/arch/x86/gdt.rs                | 27 +--------------------------
 src/arch/x86_64/gdt.rs             | 28 ++--------------------------
 src/lib.rs                         |  2 +-
 targets/x86_64-unknown-kernel.json |  3 +--
 6 files changed, 5 insertions(+), 79 deletions(-)

diff --git a/linkers/i686.ld b/linkers/i686.ld
index 386ceff5..0405e100 100644
--- a/linkers/i686.ld
+++ b/linkers/i686.ld
@@ -33,19 +33,6 @@ SECTIONS {
         . = ALIGN(4K);
     }
 
-    .tdata ALIGN(4K) : AT(ADDR(.tdata) - KERNEL_OFFSET) {
-        __bss_end = .;
-        __tdata_start = .;
-        *(.tdata*)
-        . = ALIGN(4K);
-        __tdata_end = .;
-        __tbss_start = .;
-        *(.tbss*)
-        . += 8;
-        . = ALIGN(4K);
-        __tbss_end = .;
-    }
-
     __end = .;
 
     /DISCARD/ : {
diff --git a/linkers/x86_64.ld b/linkers/x86_64.ld
index fc8a626d..9f90acf8 100644
--- a/linkers/x86_64.ld
+++ b/linkers/x86_64.ld
@@ -32,17 +32,6 @@ SECTIONS {
         *(.bss*)
     }
 
-    .tdata ALIGN(4K) : AT(ADDR(.tdata) - KERNEL_OFFSET) {
-        __bss_end = .;
-        __tdata_start = .;
-        *(.tdata*)
-        __tdata_end = .;
-        __tbss_start = .;
-        *(.tbss*)
-        . = ALIGN(4K);
-        __tbss_end = .;
-    }
-
     __end = .;
 
     /DISCARD/ : {
diff --git a/src/arch/x86/gdt.rs b/src/arch/x86/gdt.rs
index b3dc9d5e..fb73e23a 100644
--- a/src/arch/x86/gdt.rs
+++ b/src/arch/x86/gdt.rs
@@ -74,10 +74,9 @@ const BASE_GDT: [GdtEntry; 9] = [
 
 #[repr(C, align(4096))]
 pub struct ProcessorControlRegion {
-    pub tcb_end: usize,
+    pub self_ref: usize,
     pub user_rsp_tmp: usize,
     pub tss: TssWrapper,
-    pub self_ref: usize,
     pub gdt: [GdtEntry; 9],
     percpu: crate::percpu::PercpuBlock,
 }
@@ -138,8 +137,6 @@ pub unsafe fn init_paging(stack_offset: usize, cpu_id: usize) {
         base: pcr.gdt.as_ptr() as *const SegmentDescriptor,
     };
 
-    pcr.tcb_end = init_percpu();
-
     {
         let tss = &pcr.tss.0 as *const _ as usize as u32;
 
@@ -172,28 +169,6 @@ pub unsafe fn init_paging(stack_offset: usize, cpu_id: usize) {
     };
 }
 
-// TODO: Share code with x86. Maybe even with aarch64?
-/// Copy tdata, clear tbss, calculate TCB end pointer
-#[cold]
-unsafe fn init_percpu() -> usize {
-    use crate::kernel_executable_offsets::*;
-
-    let size = __tbss_end() - __tdata_start();
-    assert_eq!(size % PAGE_SIZE, 0);
-
-    let tbss_offset = __tbss_start() - __tdata_start();
-
-    let base_frame = crate::memory::allocate_frames(size / PAGE_SIZE).expect("failed to allocate percpu memory");
-    let base = RmmA::phys_to_virt(base_frame.start_address());
-
-    let tls_end = base.data() + size;
-
-    core::ptr::copy_nonoverlapping(__tdata_start() as *const u8, base.data() as *mut u8, tbss_offset);
-    core::ptr::write_bytes((base.data() + tbss_offset) as *mut u8, 0, size - tbss_offset);
-
-    tls_end
-}
-
 #[derive(Copy, Clone, Debug)]
 #[repr(packed)]
 pub struct GdtEntry {
diff --git a/src/arch/x86_64/gdt.rs b/src/arch/x86_64/gdt.rs
index 411386e8..5add48cb 100644
--- a/src/arch/x86_64/gdt.rs
+++ b/src/arch/x86_64/gdt.rs
@@ -3,7 +3,7 @@
 use core::convert::TryInto;
 use core::mem;
 
-use crate::paging::{PAGE_SIZE, RmmA, RmmArch};
+use crate::paging::{RmmA, RmmArch};
 use crate::percpu::PercpuBlock;
 
 use x86::bits64::task::TaskStateSegment;
@@ -74,12 +74,11 @@ const BASE_GDT: [GdtEntry; 8] = [
 pub struct ProcessorControlRegion {
     // TODO: When both KASLR and KPTI are implemented, the PCR may need to be split into two pages,
     // such that "secret" kernel addresses are only stored in the protected half.
+    pub self_ref: usize,
 
-    pub tcb_end: usize,
     pub user_rsp_tmp: usize,
     // TODO: The I/O permissions bitmap can require more than 8192 bytes of space.
     pub tss: TaskStateSegment,
-    pub self_ref: usize,
     // The GDT *must* be stored in the PCR! The paranoid interrupt handler, lacking a reliable way
     // to correctly obtain GSBASE, uses SGDT to calculate the PCR offset.
     pub gdt: [GdtEntry; 8],
@@ -164,8 +163,6 @@ pub unsafe fn init_paging(stack_offset: usize, cpu_id: usize) {
         base,
     };
 
-    pcr.tcb_end = init_percpu();
-
     {
         pcr.tss.iomap_base = 0xFFFF;
 
@@ -220,27 +217,6 @@ pub unsafe fn init_paging(stack_offset: usize, cpu_id: usize) {
     };
 }
 
-/// Copy tdata, clear tbss, calculate TCB end pointer
-#[cold]
-unsafe fn init_percpu() -> usize {
-    use crate::kernel_executable_offsets::*;
-
-    let size = __tbss_end() - __tdata_start();
-    assert_eq!(size % PAGE_SIZE, 0);
-
-    let tbss_offset = __tbss_start() - __tdata_start();
-
-    let base_frame = crate::memory::allocate_frames(size / PAGE_SIZE).expect("failed to allocate percpu memory");
-    let base = RmmA::phys_to_virt(base_frame.start_address());
-
-    let tls_end = base.data() + size;
-
-    core::ptr::copy_nonoverlapping(__tdata_start() as *const u8, base.data() as *mut u8, tbss_offset);
-    core::ptr::write_bytes((base.data() + tbss_offset) as *mut u8, 0, size - tbss_offset);
-
-    tls_end
-}
-
 #[derive(Copy, Clone, Debug)]
 #[repr(packed)]
 pub struct GdtEntry {
diff --git a/src/lib.rs b/src/lib.rs
index e4fcae03..e743be86 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -282,5 +282,5 @@ macro_rules! linker_offsets(
     }
 );
 pub mod kernel_executable_offsets {
-    linker_offsets!(__text_start, __text_end, __rodata_start, __rodata_end, __data_start, __data_end, __bss_start, __bss_end, __tdata_start, __tdata_end, __tbss_start, __tbss_end, __usercopy_start, __usercopy_end);
+    linker_offsets!(__text_start, __text_end, __rodata_start, __rodata_end, __data_start, __data_end, __bss_start, __bss_end, __usercopy_start, __usercopy_end);
 }
diff --git a/targets/x86_64-unknown-kernel.json b/targets/x86_64-unknown-kernel.json
index 1ab209b0..db03c07d 100644
--- a/targets/x86_64-unknown-kernel.json
+++ b/targets/x86_64-unknown-kernel.json
@@ -23,6 +23,5 @@
     "exe-suffix": "",
     "has-rpath": false,
     "no-default-libraries": true,
-    "position-independent-executables": false,
-    "tls-model": "global-dynamic"
+    "position-independent-executables": false
 }
-- 
GitLab