Initial import
[external/libunwind.git] / src / x86 / getcontext-freebsd.S
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
3
4 This file is part of libunwind.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24
25 #include "offsets.h"
26
27         .global _Ux86_getcontext
28         .type _Ux86_getcontext, @function
29 _Ux86_getcontext:
30         .cfi_startproc
31         pushl   %eax
32         .cfi_adjust_cfa_offset 4
33         mov     8(%esp),%eax  /* ucontext_t* */
34         popl    FREEBSD_UC_MCONTEXT_EAX_OFF(%eax)
35         .cfi_adjust_cfa_offset 4
36         movl    %ebx, FREEBSD_UC_MCONTEXT_EBX_OFF(%eax)
37         movl    %ecx, FREEBSD_UC_MCONTEXT_ECX_OFF(%eax)
38         movl    %edx, FREEBSD_UC_MCONTEXT_EDX_OFF(%eax)
39         movl    %edi, FREEBSD_UC_MCONTEXT_EDI_OFF(%eax)
40         movl    %esi, FREEBSD_UC_MCONTEXT_ESI_OFF(%eax)
41         movl    %ebp, FREEBSD_UC_MCONTEXT_EBP_OFF(%eax)
42
43         movl    (%esp), %ecx
44         movl    %ecx, FREEBSD_UC_MCONTEXT_EIP_OFF(%eax)
45
46         leal    4(%esp), %ecx           /* Exclude the return address.  */
47         movl    %ecx, FREEBSD_UC_MCONTEXT_ESP_OFF(%eax)
48
49         xorl    %ecx, %ecx
50         movw    %fs, %cx
51         movl    %ecx, FREEBSD_UC_MCONTEXT_FS_OFF(%eax)
52         movw    %gs, %cx
53         movl    %ecx, FREEBSD_UC_MCONTEXT_GS_OFF(%eax)
54         movw    %ds, %cx
55         movl    %ecx, FREEBSD_UC_MCONTEXT_DS_OFF(%eax)
56         movw    %es, %cx
57         movl    %ecx, FREEBSD_UC_MCONTEXT_ES_OFF(%eax)
58         movw    %ss, %cx
59         movl    %ecx, FREEBSD_UC_MCONTEXT_SS_OFF(%eax)
60         movw    %cs, %cx
61         movl    %ecx, FREEBSD_UC_MCONTEXT_CS_OFF(%eax)
62
63         pushfl
64         .cfi_adjust_cfa_offset 4
65         popl    FREEBSD_UC_MCONTEXT_EFLAGS_OFF(%eax)
66         .cfi_adjust_cfa_offset -4
67
68         movl    $0, FREEBSD_UC_MCONTEXT_TRAPNO_OFF(%eax)
69
70         movl    $FREEBSD_UC_MCONTEXT_FPOWNED_FPU,\
71                 FREEBSD_UC_MCONTEXT_OWNEDFP_OFF(%eax)
72         movl    $FREEBSD_UC_MCONTEXT_FPFMT_XMM,\
73                 FREEBSD_UC_MCONTEXT_FPFORMAT_OFF(%eax)
74
75        /*
76         * Require CPU with fxsave implemented, and enabled by OS.
77         *
78         * If passed ucontext is not aligned to 16-byte boundary,
79         * save fpu context into temporary aligned location on stack
80         * and then copy.
81         */
82         leal    FREEBSD_UC_MCONTEXT_FPSTATE_OFF(%eax), %edx
83         testl   $0xf, %edx
84         jne     2f
85         fxsave  (%edx)  /* fast path, passed ucontext save area was aligned */
86 1:      movl    $FREEBSD_UC_MCONTEXT_MC_LEN_VAL,\
87                 FREEBSD_UC_MCONTEXT_MC_LEN_OFF(%eax)
88
89         xorl    %eax, %eax
90         ret
91
92 2:      movl    %edx, %edi      /* not aligned, do the dance */
93         subl    $512 + 16, %esp /* save area and 16 bytes for alignment */
94         .cfi_adjust_cfa_offset 512 + 16
95         movl    %esp, %edx
96         orl     $0xf, %edx      /* align *%edx to 16-byte up */
97         incl    %edx
98         fxsave  (%edx)
99         movl    %edx, %esi      /* copy to the final destination */
100         movl    $512/4,%ecx
101         rep; movsl
102         addl    $512 + 16, %esp /* restore the stack */
103         .cfi_adjust_cfa_offset -512 - 16
104         movl    FREEBSD_UC_MCONTEXT_ESI_OFF(%eax), %esi
105         movl    FREEBSD_UC_MCONTEXT_EDI_OFF(%eax), %edi
106         jmp     1b
107
108         .cfi_endproc
109         .size   _Ux86_getcontext, . - _Ux86_getcontext
110
111         /* We do not need executable stack.  */
112         .section        .note.GNU-stack,"",@progbits