summaryrefslogtreecommitdiff
path: root/sys/arch
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-07-02 21:19:49 -0400
committerIan Moffett <ian@osmora.org>2024-07-02 21:25:53 -0400
commit2d7bc823503167276c6d3c40500c3055d4f38938 (patch)
tree2971c86e108f5afa1c622afa511a289d12e7a3e6 /sys/arch
parent52760e9c5e677b76c81e921bbab95da26478f425 (diff)
kernel/amd64: Handle the user GS segment register
This commit introduces usage of swapgs to switch out the GS register with the user GS register. An md_td_kick() function is also introduced to start up user threads. The this_cpu() function uses the GS register to read the current CPU structure using a new "self" field. Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys/arch')
-rw-r--r--sys/arch/amd64/amd64/machdep.c20
-rw-r--r--sys/arch/amd64/amd64/proc_machdep.c30
2 files changed, 48 insertions, 2 deletions
diff --git a/sys/arch/amd64/amd64/machdep.c b/sys/arch/amd64/amd64/machdep.c
index 82fa97b..528326a 100644
--- a/sys/arch/amd64/amd64/machdep.c
+++ b/sys/arch/amd64/amd64/machdep.c
@@ -95,20 +95,36 @@ try_mitigate_spectre(void)
struct cpu_info *
this_cpu(void)
{
- return (void *)amd64_read_gs_base();
+ struct cpu_info *ci;
+
+ /*
+ * This might look crazy but we are just leveraging the "m"
+ * constraint to calculate the offset of the self field within
+ * cpu_info. The self field points to the cpu_info structure
+ * itself allowing us to access cpu_info through %gs.
+ */
+ __ASMV("mov %%gs:%1, %0"
+ : "=r" (ci)
+ : "m" (*&((struct cpu_info *)0)->self)
+ : "memory");
+
+ return ci;
}
void
cpu_startup(struct cpu_info *ci)
{
+ ci->self = ci;
gdt_load(&bsp_gdtr);
idt_load();
setup_vectors();
amd64_write_gs_base((uintptr_t)ci);
- init_tss(ci);
+ __ASMV("swapgs"); /* Get kernel GS */
+ init_tss(ci);
try_mitigate_spectre();
+
__ASMV("sti"); /* Unmask interrupts */
lapic_init();
}
diff --git a/sys/arch/amd64/amd64/proc_machdep.c b/sys/arch/amd64/amd64/proc_machdep.c
index 2b4368b..723582b 100644
--- a/sys/arch/amd64/amd64/proc_machdep.c
+++ b/sys/arch/amd64/amd64/proc_machdep.c
@@ -34,6 +34,7 @@
#include <sys/exec.h>
#include <machine/frame.h>
#include <machine/gdt.h>
+#include <machine/cpu.h>
#include <vm/physmem.h>
#include <vm/vm.h>
#include <vm/map.h>
@@ -113,6 +114,35 @@ setregs(struct proc *td, struct exec_prog *prog, uintptr_t stack)
}
/*
+ * Startup a user thread.
+ *
+ * @td: Thead to start up.
+ */
+void
+md_td_kick(struct proc *td)
+{
+ struct trapframe *tfp;
+
+ tfp = &td->tf;
+ __ASMV(
+ "push %0\n"
+ "push %1\n"
+ "pushf\n"
+ "push %2\n"
+ "push %3\n"
+ "swapgs\n"
+ "iretq"
+ :
+ : "i" (USER_DS | 3),
+ "r" (tfp->rsp),
+ "i" (USER_CS | 3),
+ "r" (tfp->rip)
+ );
+
+ __builtin_unreachable();
+}
+
+/*
* MD thread init code
*
* @p: New process.