Browse Source

Adapt to toolchains (unfinished)

徐启航 2 năm trước cách đây
mục cha
commit
805e1d6a5a

+ 1 - 1
h2o/kernel/.cargo/x86_64-h2o-kernel.json

@@ -1,6 +1,6 @@
 {
     "llvm-target": "x86_64-unknown-none",
-    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
+    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
     "arch": "x86_64",
     "target-endian": "little",
     "target-pointer-width": "64",

+ 1 - 4
h2o/kernel/src/cpu/x86_64/syscall.rs

@@ -2,7 +2,6 @@ use core::mem::size_of;
 
 use archop::{msr, reg};
 use paging::LAddr;
-use sv_call::SerdeReg;
 
 use super::seg::ndt::{INTR_CODE, USR_CODE_X86};
 use crate::sched::task::ctx::arch::Frame;
@@ -39,9 +38,7 @@ unsafe extern "C" fn hdl_syscall(frame: *const Frame) {
     archop::pause_intr();
 
     let _ = crate::sched::SCHED.with_current(|cur| {
-        cur.kstack_mut()
-            .task_frame_mut()
-            .set_syscall_retval(res.encode());
+        cur.kstack_mut().task_frame_mut().set_syscall_retval(res);
         Ok(())
     });
 }

+ 1 - 1
h2o/libs/syscall/build.rs

@@ -1,7 +1,7 @@
 use std::error::Error;
 
 fn main() -> Result<(), Box<dyn Error>> {
-    #[cfg(all(feature = "call", not(feature = "vdso")))]
+    #[cfg(all(feature = "call", feature = "vdso"))]
     {
         let config = cbindgen::Config::from_file("cbindgen.toml")?;
         println!("cargo:rerun-if-changed=cbindgen.toml");

+ 1 - 1
h2o/tinit/.cargo/x86_64-h2o-tinit.json

@@ -1,6 +1,6 @@
 {
     "llvm-target": "x86_64-h2o-tinit",
-    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
+    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
     "arch": "x86_64",
     "target-endian": "little",
     "target-pointer-width": "64",

+ 2 - 3
src/lib/libc/crt/crt0.asm

@@ -1,9 +1,8 @@
 global _start:function
 
 extern __libc_start_main
+extern main
 
 _start:
+    lea rsi, [rel main]
     jmp __libc_start_main
-
-
-

+ 3 - 2
src/lib/libc/ldso/src/dso.rs

@@ -168,12 +168,13 @@ impl Dso {
 
     pub fn load(
         phys: &Phys,
-        name: CString,
+        name: impl Into<CString>,
         prog: bool,
     ) -> Result<(LoadedElf, NonNull<Dso>), Error> {
         let mut dso_list = dso_list().lock();
         let names = dso_list.names.clone();
-        Self::load_dso(phys, name, prog, &mut dso_list).inspect_err(|_| dso_list.names = names)
+        Self::load_dso(phys, name.into(), prog, &mut dso_list)
+            .inspect_err(|_| dso_list.names = names)
     }
 
     fn load_dso(

+ 1 - 2
src/lib/libc/ldso/src/lib.rs

@@ -41,8 +41,7 @@ fn dl_main(init_chan: Channel) -> rxx::DlReturn {
     let prog = take_startup_handle(HandleInfo::new().with_handle_type(HandleType::ProgramPhys));
     let prog = unsafe { Phys::from_raw(prog) };
 
-    let (elf, _) =
-        dso::Dso::load(&prog, cstr!("<PROGRAM>").into(), true).expect("Failed to load program");
+    let (elf, _) = dso::Dso::load(&prog, cstr!("<PROGRAM>"), true).expect("Failed to load program");
 
     log::debug!("Reaching end of the dynamic linker");
 

+ 6 - 1
src/lib/libc/ldso/src/rxx.rs

@@ -1,4 +1,7 @@
-use core::sync::atomic::{self, Ordering::SeqCst};
+use core::{
+    mem,
+    sync::atomic::{self, Ordering::SeqCst},
+};
 
 use solvent::prelude::{Channel, Handle, Object};
 
@@ -98,8 +101,10 @@ unsafe extern "C" fn dl_start(init_chan: Handle, vdso_map: usize) -> DlReturn {
         match d_tag {
             DT_REL => rel = Some((base as *mut u8).add(d_val)),
             DT_RELCOUNT => crel = Some(d_val),
+            DT_RELSZ => crel = crel.or(Some(d_val / mem::size_of::<Rel>())),
             DT_RELA => rela = Some((base as *mut u8).add(d_val)),
             DT_RELACOUNT => crela = Some(d_val),
+            DT_RELASZ => crela = crela.or(Some(d_val / mem::size_of::<Rela>())),
             DT_RELR => relr = Some((base as *mut u8).add(d_val)),
             DT_RELRSZ => szrelr = Some(d_val),
             _ => {}

+ 82 - 7
src/lib/libc/src/ffi/string.rs

@@ -270,16 +270,91 @@ pub unsafe extern "C" fn memchr(ptr: *const c_void, ch: c_int, count: usize) ->
     pos.map_or(ptr::null(), |pos| unsafe { ptr.add(pos) })
 }
 
-// Defined in `compiler_builtins`. TODO: implement this self with SIMD
-// optimizations.
-extern "C" {
-    pub fn memcmp(lhs: *const c_void, rhs: *const c_void, count: usize) -> c_int;
+// TODO: implement these with SIMD optimizations.
 
-    pub fn memset(dest: *mut c_void, ch: c_int, count: usize) -> *mut c_void;
+#[no_mangle]
+pub unsafe extern "C" fn memcmp(lhs: *const c_void, rhs: *const c_void, count: usize) -> c_int {
+    let (lhs, rhs) = (lhs as *const u8, rhs as *const u8);
+    let mut i = 0;
+    while i < count {
+        let a = *lhs.add(i);
+        let b = *rhs.add(i);
+        if a != b {
+            return a as i32 - b as i32;
+        }
+        i += 1;
+    }
+    0
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn memset(dest: *mut c_void, ch: c_int, count: usize) -> *mut c_void {
+    let qword_count = count >> 3;
+    let byte_count = count & 0b111;
+    core::arch::asm!(
+        "repe stosq %rax, (%rdi)",
+        "mov {byte_count:e}, %ecx",
+        "repe stosb %al, (%rdi)",
+        byte_count = in(reg) byte_count,
+        inout("rcx") qword_count => _,
+        inout("rdi") dest => _,
+        in("rax") (ch as u64) * 0x0101010101010101,
+        options(att_syntax, nostack, preserves_flags)
+    );
+    dest
+}
 
-    pub fn memcpy(dest: *mut c_void, src: *const c_void, count: usize) -> *mut c_void;
+#[no_mangle]
+pub unsafe extern "C" fn memcpy(
+    dest: *mut c_void,
+    src: *const c_void,
+    count: usize,
+) -> *mut c_void {
+    let qword_count = count >> 3;
+    let byte_count = count & 0b111;
+    core::arch::asm!(
+        "repe movsq (%rsi), (%rdi)",
+        "mov {byte_count:e}, %ecx",
+        "repe movsb (%rsi), (%rdi)",
+        byte_count = in(reg) byte_count,
+        inout("rcx") qword_count => _,
+        inout("rdi") dest => _,
+        inout("rsi") src => _,
+        options(att_syntax, nostack, preserves_flags)
+    );
+    dest
+}
 
-    pub fn memmove(dest: *mut c_void, src: *const c_void, count: usize) -> *mut c_void;
+#[no_mangle]
+pub unsafe extern "C" fn memmove(
+    dest: *mut c_void,
+    src: *const c_void,
+    count: usize,
+) -> *mut c_void {
+    let delta = (dest as usize).wrapping_sub(src as usize);
+    if delta >= count {
+        // We can copy forwards because either dest is far enough ahead of src,
+        // or src is ahead of dest (and delta overflowed).
+        memcpy(dest, src, count)
+    } else {
+        let qword_count = count >> 3;
+        let byte_count = count & 0b111;
+        core::arch::asm!(
+            "std",
+            "repe movsq (%rsi), (%rdi)",
+            "movl {byte_count:e}, %ecx",
+            "addq $7, %rdi",
+            "addq $7, %rsi",
+            "repe movsb (%rsi), (%rdi)",
+            "cld",
+            byte_count = in(reg) byte_count,
+            inout("rcx") qword_count => _,
+            inout("rdi") dest.add(count).wrapping_sub(8) => _,
+            inout("rsi") src.add(count).wrapping_sub(8) => _,
+            options(att_syntax, nostack)
+        );
+        dest
+    }
 }
 
 #[no_mangle]