summaryrefslogtreecommitdiffstats
path: root/sys/amd64/amd64/cpu_switch.S
diff options
context:
space:
mode:
authorkib <kib@FreeBSD.org>2009-04-01 13:09:26 +0000
committerkib <kib@FreeBSD.org>2009-04-01 13:09:26 +0000
commit253f7779bc4142bb2814f0f44999bc0e48e9bbe5 (patch)
treeb58cda2045e48f43c2d8ff644f3d5c3a859f6e99 /sys/amd64/amd64/cpu_switch.S
parentd2c67f9684a76233c58b569abc3a56c24f6a64db (diff)
downloadFreeBSD-src-253f7779bc4142bb2814f0f44999bc0e48e9bbe5.zip
FreeBSD-src-253f7779bc4142bb2814f0f44999bc0e48e9bbe5.tar.gz
Save and restore segment registers on amd64 when entering and leaving
the kernel on amd64. Fill and read segment registers for mcontext and signals. Handle traps caused by restoration of the invalidated selectors. Implement user-mode creation and manipulation of the process-specific LDT descriptors for amd64, see sysarch(2). Implement support for TSS i/o port access permission bitmap for amd64. Context-switch LDT and TSS. Do not save and restore segment registers on the context switch, that is handled by kernel enter/leave trampolines now. Remove segment restore code from the signal trampolines for freebsd/amd64, freebsd/ia32 and linux/i386 for the same reason. Implement amd64-specific compat shims for sysarch. Linuxolator (temporary ?) switched to use gsbase for thread_area pointer. TODO: Currently, gdb is not adapted to show segment registers from struct reg. Also, no machine-depended ptrace command is added to set segment registers for debugged process. In collaboration with: pho Discussed with: peter Reviewed by: jhb Linuxolator tested by: dchagin
Diffstat (limited to 'sys/amd64/amd64/cpu_switch.S')
-rw-r--r--sys/amd64/amd64/cpu_switch.S153
1 files changed, 59 insertions, 94 deletions
diff --git a/sys/amd64/amd64/cpu_switch.S b/sys/amd64/amd64/cpu_switch.S
index 0c59703..6fc8290 100644
--- a/sys/amd64/amd64/cpu_switch.S
+++ b/sys/amd64/amd64/cpu_switch.S
@@ -75,8 +75,6 @@ ENTRY(cpu_throw)
1:
movq TD_PCB(%rdi),%r8 /* Old pcb */
movl PCPU(CPUID), %eax
- movq PCB_FSBASE(%r8),%r9
- movq PCB_GSBASE(%r8),%r10
/* release bit from old pm_active */
movq TD_PROC(%rdi), %rdx /* oldtd->td_proc */
movq P_VMSPACE(%rdx), %rdx /* proc->p_vmspace */
@@ -110,28 +108,6 @@ ENTRY(cpu_switch)
movq %rbx,PCB_RBX(%r8)
movq %rax,PCB_RIP(%r8)
- /*
- * Reread fs and gs bases. Explicit fs segment register load
- * by the usermode code may change actual fs base without
- * updating pcb_{fs,gs}base.
- *
- * %rdx still contains the mtx, save %rdx around rdmsr.
- */
- movq %rdx,%r11
- movl $MSR_FSBASE,%ecx
- rdmsr
- shlq $32,%rdx
- leaq (%rax,%rdx),%r9
- movl $MSR_KGSBASE,%ecx
- rdmsr
- shlq $32,%rdx
- leaq (%rax,%rdx),%r10
- movq %r11,%rdx
-
- testl $PCB_32BIT,PCB_FLAGS(%r8)
- jnz store_seg
-done_store_seg:
-
testl $PCB_DBREGS,PCB_FLAGS(%r8)
jnz store_dr /* static predict not taken */
done_store_dr:
@@ -192,36 +168,47 @@ sw1:
testl $TDP_KTHREAD,TD_PFLAGS(%rsi)
jnz do_kthread
- testl $PCB_32BIT,PCB_FLAGS(%r8)
- jnz load_seg
-done_load_seg:
+ /*
+ * Load ldt register
+ */
+ movq TD_PROC(%rsi),%rcx
+ cmpq $0, P_MD+MD_LDT(%rcx)
+ jne do_ldt
+ xorl %eax,%eax
+ld_ldt: lldt %ax
- cmpq PCB_FSBASE(%r8),%r9
- jz 1f
- /* Restore userland %fs */
-restore_fsbase:
- movl $MSR_FSBASE,%ecx
+ /* Restore fs base in GDT */
movl PCB_FSBASE(%r8),%eax
- movl PCB_FSBASE+4(%r8),%edx
- wrmsr
-1:
- cmpq PCB_GSBASE(%r8),%r10
- jz 2f
- /* Restore userland %gs */
- movl $MSR_KGSBASE,%ecx
+ movq PCPU(FS32P),%rdx
+ movw %ax,2(%rdx)
+ shrl $16,%eax
+ movb %al,4(%rdx)
+ shrl $8,%eax
+ movb %al,7(%rdx)
+
+ /* Restore gs base in GDT */
movl PCB_GSBASE(%r8),%eax
- movl PCB_GSBASE+4(%r8),%edx
- wrmsr
-2:
+ movq PCPU(GS32P),%rdx
+ movw %ax,2(%rdx)
+ shrl $16,%eax
+ movb %al,4(%rdx)
+ shrl $8,%eax
+ movb %al,7(%rdx)
-do_tss:
+do_kthread:
+ /* Do we need to reload tss ? */
+ movq PCPU(TSSP),%rax
+ movq PCB_TSSP(%r8),%rdx
+ testq %rdx,%rdx
+ cmovzq PCPU(COMMONTSSP),%rdx
+ cmpq %rax,%rdx
+ jne do_tss
+done_tss:
+ movq %r8,PCPU(RSP0)
+ movq %r8,PCPU(CURPCB)
/* Update the TSS_RSP0 pointer for the next interrupt */
- movq PCPU(TSSP), %rax
- movq %r8, PCPU(RSP0)
- movq %r8, PCPU(CURPCB)
- addq $COMMON_TSS_RSP0, %rax
- movq %rsi, PCPU(CURTHREAD) /* into next thread */
- movq %r8, (%rax)
+ movq %r8,COMMON_TSS_RSP0(%rdx)
+ movq %rsi,PCPU(CURTHREAD) /* into next thread */
/* Test if debug registers should be restored. */
testl $PCB_DBREGS,PCB_FLAGS(%r8)
@@ -250,45 +237,6 @@ done_load_dr:
* We use jumps rather than call in order to avoid the stack.
*/
-do_kthread:
- /*
- * Copy old fs/gsbase to new kthread pcb for future switches
- * This maintains curpcb->pcb_[fg]sbase as caches of the MSR
- */
- movq %r9,PCB_FSBASE(%r8)
- movq %r10,PCB_GSBASE(%r8)
- jmp do_tss
-
-store_seg:
- mov %gs,PCB_GS(%r8)
- testl $PCB_GS32BIT,PCB_FLAGS(%r8)
- jnz 2f
-1: mov %ds,PCB_DS(%r8)
- mov %es,PCB_ES(%r8)
- mov %fs,PCB_FS(%r8)
- jmp done_store_seg
-2: movq PCPU(GS32P),%rax
- movq (%rax),%rax
- movq %rax,PCB_GS32SD(%r8)
- jmp 1b
-
-load_seg:
- testl $PCB_GS32BIT,PCB_FLAGS(%r8)
- jnz 2f
-1: movl $MSR_GSBASE,%ecx
- rdmsr
- mov PCB_GS(%r8),%gs
- wrmsr
- mov PCB_DS(%r8),%ds
- mov PCB_ES(%r8),%es
- mov PCB_FS(%r8),%fs
- jmp restore_fsbase
- /* Restore userland %gs while preserving kernel gsbase */
-2: movq PCPU(GS32P),%rax
- movq PCB_GS32SD(%r8),%rcx
- movq %rcx,(%rax)
- jmp 1b
-
store_dr:
movq %dr7,%rax /* yes, do the save */
movq %dr0,%r15
@@ -325,6 +273,29 @@ load_dr:
movq %r11,%dr6
movq %rax,%dr7
jmp done_load_dr
+
+do_tss: movq %rdx,PCPU(TSSP)
+ movq %rdx,%rcx
+ movq PCPU(TSS),%rax
+ movw %rcx,2(%rax)
+ shrq $16,%rcx
+ movb %cl,4(%rax)
+ shrq $8,%rcx
+ movb %cl,7(%rax)
+ shrq $8,%rcx
+ movl %ecx,8(%rax)
+ movb $0x89,5(%rax) /* unset busy */
+ movl $TSSSEL,%eax
+ ltr %ax
+ jmp done_tss
+
+do_ldt: movq PCPU(LDT),%rax
+ movq P_MD+MD_LDT_SD(%rcx),%rdx
+ movq %rdx,(%rax)
+ movq P_MD+MD_LDT_SD+8(%rcx),%rdx
+ movq %rdx,8(%rax)
+ movl $LDTSEL,%eax
+ jmp ld_ldt
END(cpu_switch)
/*
@@ -398,12 +369,6 @@ ENTRY(savectx2)
movq (%rsp),%rax
movq %rax,PCB_RIP(%r8)
- mov %ds,PCB_DS(%r8)
- mov %es,PCB_ES(%r8)
- mov %ss,XPCB_SS(%r8)
- mov %fs,PCB_FS(%r8)
- mov %gs,PCB_GS(%r8)
-
movq %rbx,PCB_RBX(%r8)
movq %rsp,PCB_RSP(%r8)
movq %rbp,PCB_RBP(%r8)
OpenPOWER on IntegriCloud