Skip to content

Commit

Permalink
start to stack switching single processes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewfl committed Jul 6, 2016
1 parent eefebeb commit 304f38a
Show file tree
Hide file tree
Showing 21 changed files with 1,921 additions and 945 deletions.
11 changes: 7 additions & 4 deletions make
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ def compile():
f.replace('src', 'build').replace('.cc', '.o'),
CXX=CXX
))
Run('{CC} -c src/asm.s -o build/asm.o'.format(
CC=CC
))
Run('{CC} -c src/asm_snippets.s -o build/asm_snippets.o'.format(
# Run('{CC} -c src/asm.s -o build/asm.o'.format(
# CC=CC
# ))
# Run('{CC} -c src/asm_snippets.s -o build/asm_snippets.o'.format(
# CC=CC
# ))
Run('{CC} -c src/asm_interface.s -o build/asm_snippets.o'.format(
CC=CC
))
after()
Expand Down
98 changes: 98 additions & 0 deletions src/asm_context_switch.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright Oliver Kowalke 2009.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt)
*/

/****************************************************************************************
* *
* ---------------------------------------------------------------------------------- *
* | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | *
* ---------------------------------------------------------------------------------- *
* | 0x0 | 0x4 | 0x8 | 0xc | 0x10 | 0x14 | 0x18 | 0x1c | *
* ---------------------------------------------------------------------------------- *
* | fc_mxcsr|fc_x87_cw| R12 | R13 | R14 | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | *
* ---------------------------------------------------------------------------------- *
* | 0x20 | 0x24 | 0x28 | 0x2c | 0x30 | 0x34 | 0x38 | 0x3c | *
* ---------------------------------------------------------------------------------- *
* | R15 | RBX | RBP | RIP | *
* ---------------------------------------------------------------------------------- *
* ---------------------------------------------------------------------------------- *
* | 16 | 17 | | *
* ---------------------------------------------------------------------------------- *
* | 0x40 | 0x44 | | *
* ---------------------------------------------------------------------------------- *
* | EXIT | | *
* ---------------------------------------------------------------------------------- *
* *
****************************************************************************************/

.text
.globl jump_fcontext
.type jump_fcontext,@function
.align 16
jump_fcontext:
pushq %rbp /* save RBP */
pushq %rbx /* save RBX */
pushq %r15 /* save R15 */
pushq %r14 /* save R14 */
pushq %r13 /* save R13 */
pushq %r12 /* save R12 */

/* prepare stack for FPU */
leaq -0x8(%rsp), %rsp

/* test for flag preserve_fpu */
cmp $0, %rcx
je 1f

/* save MMX control- and status-word */
stmxcsr (%rsp)
/* save x87 control-word */
fnstcw 0x4(%rsp)

1:
/* store RSP (pointing to context-data) in RDI */
movq %rsp, (%rdi)

/* restore RSP (pointing to context-data) from RSI */
movq %rsi, %rsp

/* test for flag preserve_fpu */
cmp $0, %rcx
je 2f

/* restore MMX control- and status-word */
ldmxcsr (%rsp)
/* restore x87 control-word */
fldcw 0x4(%rsp)

2:
/* prepare stack for FPU */
leaq 0x8(%rsp), %rsp

popq %r12 /* restrore R12 */
popq %r13 /* restrore R13 */
popq %r14 /* restrore R14 */
popq %r15 /* restrore R15 */
popq %rbx /* restrore RBX */
popq %rbp /* restrore RBP */

/* restore return-address */
popq %r8

/* use third arg as return-value after jump */
movq %rdx, %rax
/* use third arg as first arg in context function */
movq %rdx, %rdi

/* indirect jump to context */
jmp *%r8
.size jump_fcontext,.-jump_fcontext

/* Mark that we don't need executable stack. */
.section .note.GNU-stack,"",%progbits
147 changes: 147 additions & 0 deletions src/asm_interface.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* argument registers: rdi, rsi, rdx, rcx, r8, r9
* preserve registers: rbx, rsp, rbp, r12, r13, r14, r15
* scratch registers: rax, rdi, rsi, rdx, rcx, r8, r9, r10, r11
*/

////////////////////////////////////////////////////
.macro m_push_all_regs
// should match the direction in sys/regs.h
push %r15
push %r14
push %r13
push %rbp
push %rbx
push %r11
push %r10
push %r9
push %r8
push %rax
push %rcx
push %rdx
push %rsi
push %rdi
// orig rax??
// push %rip (bad instruction)t
// push %cs (bad instruction)
sub $24, %rsp

//push %eflags
pushf

// stack pointer? ignore
push %rsp
// push %ss (bad instruction)
// fsbase (?)
// gsbase (?)
// push %ds (bad instruction)
// push %es (bad instruction)
sub $40, %rsp

push %fs
push %gs
.endm

//////////////////////////////////////////////////
.macro m_pop_all_regs
pop %gs
pop %fs

add $48, %rsp

// ignore the stack pointer

popf

add $24, %rsp
pop %rdi
pop %rsi
pop %rdx
pop %rcx
pop %rax
pop %r8
pop %r9
pop %r10
pop %r11
pop %rbx
pop %rbp
pop %r13
pop %r14
pop %r15
.endm



.global red_asm_push_all_regs_start
.global red_asm_push_all_regs_end
red_asm_push_all_regs_start:
m_push_all_regs
red_asm_push_all_regs_end:


.global red_asm_pop_all_regs_start
.global red_asm_pop_all_regs_end
red_asm_pop_all_regs_start:
m_pop_all_regs
red_asm_pop_all_regs_end:


.global red_asm_resume_tracer_block_start
.global red_asm_resume_tracer_block_end
red_asm_resume_tracer_block_start:
sub $8, %rsp
m_push_all_regs
movq %rsp, %rax
movq $0xfafafafafafafafa, %rsp
movq 0(%rsp), %r12
movq 8(%rsp), %r13
movq 16(%rsp), %r14
movq 24(%rsp), %r15
movq 32(%rsp), %rbx
movq 40(%rsp), %rbp

movq 56(%rsp), %rsp

ret
red_asm_resume_tracer_block_end:

.global red_asm_resume_eval_block
red_asm_resume_eval_block:
movq %rsp, 56(%rdi)

movq %r12, 0(%rdi)
movq %r13, 8(%rdi)
movq %r14, 16(%rdi)
movq %r15, 24(%rdi)
movq %rbx, 32(%rdi)
movq %rbp, 40(%rdi)

movq %rsi, %rsp
m_pop_all_regs
ret


.global red_asm_return_to_block
red_asm_return_to_block:
mov %rax, %rsp
m_pop_all_regs
ret

.global red_asm_start_tracing
red_asm_start_tracing:
// [null (old stack pointer), method_to_call, tracer_this, new_stack]
sub $8, %rsp
m_push_all_regs
mov %rsp, %rdi
mov %rcx, %rsp
jmp *%rsi



.global red_asm_compile_buff_near
red_asm_compile_buff_near:
ret


// we don't need executable stack
.section .note.GNU-stack,"",%progbits
Loading

0 comments on commit 304f38a

Please sign in to comment.