installer: Finish incomplete commit to merge nrelease/{installer,root}.
[dragonfly.git] / lib / libc / amd64 / gen / mcontext.S
1 /*
2  * Copyright (c) 2007 Matthew T. Emmerton <matt@gsicomp.on.ca>
3  * All rights reserved.
4  * Copyright (c) 2007 Matthew Dillon <dillon@backplane.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Neither the name of the author nor the names of its contributors
13  *    may be used to endorse or promote products derived from this software
14  *    without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $DragonFly: src/lib/libc/amd64/gen/mcontext.S,v 1.2 2008/08/28 23:36:31 dillon Exp $
29  */
30
31 #include <machine/asm.h>
32 #include <asmcontext.h>
33
34         /*
35          * int get_mcontext(mcontext_t *mcp : rdi)
36          *
37          * Copy the caller's context into the mcontext, %eax excepted.
38          */
39         .weak   get_mcontext
40         .set    get_mcontext,_get_mcontext
41 ENTRY(_get_mcontext)
42         /*      MC_ONSTACK(%rdi)        */
43         movq    %rdi,MC_RDI(%rdi)
44         movq    %rsi,MC_RSI(%rdi)
45         movq    %rdx,MC_RDX(%rdi)
46         movq    %r8,MC_R8(%rdi)
47         movq    %r9,MC_R9(%rdi)
48         /* movq %rax,MC_RAX(%rdi) - not needed, replaced below */
49         movq    %rbx,MC_RBX(%rdi)
50         movq    %rbp,MC_RBP(%rdi)
51         movq    %r10,MC_R10(%rdi)
52         movq    %r11,MC_R11(%rdi)
53         movq    %r12,MC_R12(%rdi)
54         movq    %r13,MC_R13(%rdi)
55         movq    %r14,MC_R14(%rdi)
56         movq    %r15,MC_R15(%rdi)
57         /*      MC_TRAPNO(%rdi) */
58         /*      MC_ADDR(%rdi)   */
59         /*      MC_FLAGS(%rdi)  */
60         /*      MC_ERR(%rdi)    */
61         /*      MC_RIP(%rdi)    - see below */
62         /*      MC_CS(%rdi)     */
63         /*      MC_RFLAGS(%rdi) */
64         /*      MC_RSP(%rdi)    - see below */
65         /*      MC_SS(%rdi)     */
66
67         /*
68          * Saved stack pointer as if we had returned from this
69          * procedure.
70          */
71         movq    %rsp,MC_RSP(%eax)
72         addq    $8,MC_RSP(%eax)
73
74         /*
75          * Save eflags
76          * XXX
77          */
78
79         /*
80          * Saved instruction pointer as if we had returned from
81          * this procedure.
82          */
83         movq    (%rsp),%rdx
84         movq    %rdx,MC_RIP(%rdi)
85
86         /*
87          * On restore as if procedure returned the value 1
88          */
89         movq    $1,MC_RAX(%rdi) 
90
91         /*
92          * Set MC_LEN
93          */
94         movl    $SIZEOF_MCONTEXT_T,MC_LEN(%eax)
95
96         /*
97          * Return 0
98          */
99         subq    %rax,%rax
100         ret
101
102
103         /*
104          * int set_mcontext(mcontext_t *mcp)
105          *
106          * Load the register context, effectively switching to the
107          * new context.
108          */
109         .weak   set_mcontext
110         .set    set_mcontext,_set_mcontext
111 ENTRY(_set_mcontext)
112         /*      MC_ONSTACK(%rdi)        */
113         /*      MC_RDI(%rdi)    - see below */
114         movq    MC_RSI(%rdi),%rsi
115         movq    MC_RDX(%rdi),%rdx
116         movq    MC_R8(%rdi),%r8
117         movq    MC_R9(%rdi),%r9
118         /*      MC_RAX(%rdi)    - see below */
119         movq    MC_RBX(%rdi),%rbx
120         movq    MC_RBP(%rdi),%rbp
121         movq    MC_R10(%rdi),%r10
122         movq    MC_R11(%rdi),%r11
123         movq    MC_R12(%rdi),%r12
124         movq    MC_R13(%rdi),%r13
125         movq    MC_R14(%rdi),%r14
126         movq    MC_R15(%rdi),%r15
127         /*      MC_TRAPNO(%rdi) */
128         /*      MC_ADDR(%rdi)   */
129         /*      MC_FLAGS(%rdi)  */
130         /*      MC_ERR(%rdi)    */
131         /*      MC_RIP(%rdi)    - see below */
132         /*      MC_CS(%rdi)     */
133         /*      MC_RFLAGS(%rdi) */
134         /*      MC_RSP(%rdi)    - see below */
135         /*      MC_SS(%rdi)     */
136
137         /*
138          * Load the new stack pointer
139          */
140         movq    MC_RSP(%rdi),%rsp
141
142         /*
143          * Restore the flags
144          * XXX
145          */
146
147         /*
148          * Push the return pc so we can 'ret' to it.
149          */
150         pushq   MC_RIP(%rdi)
151
152         /*
153          * Finally rax and rdi
154          */
155         movq    MC_RAX(%rdi),%rax
156         movq    MC_RDI(%rdi),%rdi
157         retq
158