| 1 | /*
|
|---|
| 2 | * SPDX-FileCopyrightText: 2001-2004 Jakub Jermar
|
|---|
| 3 | *
|
|---|
| 4 | * SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | #include <abi/asmtool.h>
|
|---|
| 8 | #include <libarch/fibril_context.h>
|
|---|
| 9 |
|
|---|
| 10 | .text
|
|---|
| 11 |
|
|---|
| 12 | ## Save current CPU context
|
|---|
| 13 | #
|
|---|
| 14 | # Save CPU context to context_t variable
|
|---|
| 15 | # pointed by the 1st argument. Returns 0 in RAX.
|
|---|
| 16 | #
|
|---|
| 17 | FUNCTION_BEGIN(__context_save)
|
|---|
| 18 | movq (%rsp), %rdx # the caller's return %eip
|
|---|
| 19 |
|
|---|
| 20 | # in %rdi is passed 1st argument
|
|---|
| 21 | movq %rdx, __CONTEXT_OFFSET_PC(%rdi)
|
|---|
| 22 | movq %rsp, __CONTEXT_OFFSET_SP(%rdi)
|
|---|
| 23 |
|
|---|
| 24 | movq %rbx, __CONTEXT_OFFSET_RBX(%rdi)
|
|---|
| 25 | movq %rbp, __CONTEXT_OFFSET_RBP(%rdi)
|
|---|
| 26 | movq %r12, __CONTEXT_OFFSET_R12(%rdi)
|
|---|
| 27 | movq %r13, __CONTEXT_OFFSET_R13(%rdi)
|
|---|
| 28 | movq %r14, __CONTEXT_OFFSET_R14(%rdi)
|
|---|
| 29 | movq %r15, __CONTEXT_OFFSET_R15(%rdi)
|
|---|
| 30 |
|
|---|
| 31 | movq %fs:0, %rax
|
|---|
| 32 | movq %rax, __CONTEXT_OFFSET_TLS(%rdi)
|
|---|
| 33 |
|
|---|
| 34 | xorq %rax, %rax # __context_save returns 0
|
|---|
| 35 | ret
|
|---|
| 36 | FUNCTION_END(__context_save)
|
|---|
| 37 |
|
|---|
| 38 | ## Restore current CPU context
|
|---|
| 39 | #
|
|---|
| 40 | # Restore CPU context from context_t variable
|
|---|
| 41 | # pointed by the 1st argument. Returns second argument in RAX.
|
|---|
| 42 | #
|
|---|
| 43 | FUNCTION_BEGIN(__context_restore)
|
|---|
| 44 | movq __CONTEXT_OFFSET_R15(%rdi), %r15
|
|---|
| 45 | movq __CONTEXT_OFFSET_R14(%rdi), %r14
|
|---|
| 46 | movq __CONTEXT_OFFSET_R13(%rdi), %r13
|
|---|
| 47 | movq __CONTEXT_OFFSET_R12(%rdi), %r12
|
|---|
| 48 | movq __CONTEXT_OFFSET_RBP(%rdi), %rbp
|
|---|
| 49 | movq __CONTEXT_OFFSET_RBX(%rdi), %rbx
|
|---|
| 50 |
|
|---|
| 51 | movq __CONTEXT_OFFSET_SP(%rdi), %rsp # ctx->sp -> %rsp
|
|---|
| 52 |
|
|---|
| 53 | movq __CONTEXT_OFFSET_PC(%rdi), %rdx
|
|---|
| 54 |
|
|---|
| 55 | movq %rdx,(%rsp)
|
|---|
| 56 |
|
|---|
| 57 | movq __CONTEXT_OFFSET_TLS(%rdi), %rdi
|
|---|
| 58 | movq %rdi, %fs:0
|
|---|
| 59 |
|
|---|
| 60 | movq %rsi, %rax # __context_restore returns second argument
|
|---|
| 61 | ret
|
|---|
| 62 | FUNCTION_END(__context_restore)
|
|---|
| 63 |
|
|---|