blob: da226c99b2ca31903015248227db9cd345528196 [file] [log] [blame]
David 'Digit' Turnerc124baa2012-07-12 19:06:15 +02001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#ifndef _ARCH_X86_SYS_UCONTEXT_H_
29#define _ARCH_X86_SYS_UCONTEXT_H_
30
31#include <signal.h>
32#include <stdint.h>
33
34__BEGIN_DECLS
35
36/* Technical note:
37 *
38 * On x86, there are differences in the way GLibc and the Linux kernel declare
39 * ucontext_t. Here are the details (they differ from ARM significantly):
40 *
41 * - The kernel implements 'uc_mcontext' with a 'struct sigcontext',
42 * while GLibc defines 'mcontext_t' in a binary-compatible way
43 * (same size, same binary layout), but with different field naming/access
44 * conventions.
45 *
46 * The biggest difference is that 'struct sigcontext' requires named fields
47 * like "eax", or "ebp" to access the state of register values, while GLibc
48 * provides an array (gregs[]) of 32-bit values, that can be accessed
49 * through indexing, plus some enum/macros like REG_EAX or REG_ESP to get
50 * to specific register values.
51 *
52 * For maximum portability of existing client code, this header follows
53 * the GLibc convention. Client code, and the C library, should never
54 * try to include <asm/ucontext.h>
55 *
56 * - The floating-point state is not stored in the mcontext_t. For historical
57 * reasons, the 'fpregs' field of 'mcontext_t' is really a pointer to a
58 * different block of memory. More specifically:
59 *
60 * * When passed to a signal handler, the pointer points to some memory
61 * on the signal stack, populated by the kernel before calling the
62 * handler in user space.
63 *
64 * * In GLibc's getcontext()/setcontext() implementation, the pointer will
65 * point to the ucontext_t structure (more specifically the __fpregs_mem
66 * field after uc_sigmask).
67 *
68 * * Note that in both cases, the pointer can be NULL if no floating-point
69 * operation has been performed on the current thread yet.
70 *
71 * - The 'struct _libc_fpstate' structure declared by GLibc corresponds to
72 * both the FP state managed by getcontext()/setcontext(), and the start
73 * of the FP state stored by the kernel and passed to a signal handler.
74 *
75 * This matches the data saved by the FNSTENV IA-32 instruction.
76 *
77 * IMPORTANT: GLibc does not save the MMX/SSE state, i.e. it does not use
78 * FXSAVE / FXRSTOR instructions in *context() functions.
79 *
80 * It is used by client code to retrieve the state of the FPU registers
81 * (e.g. Google Breakpad does that).
82 *
83 * Reference source files:
84 * $KERNEL/arch/x86/include/asm/sigcontext_32.h
85 * $KERNEL/arch/x86/include/asm/sigcontext.h
86 * $KERNEL/arch/x86/include/asm/ucontext.h
87 *
88 * $GLIBC/sysdeps/unix/sysv/linux/i386/getcontext.S
89 * $GLIBC/sysdeps/unix/sysv/linux/i386/setcontext.S
90 * $GLIBC/sysdeps/unix/sysv/linux/x86/sys/ucontext.h
91 */
92
93/* First, the kernel-compatible version, for reference. */
94typedef struct __kernel_ucontext {
95 unsigned long uc_flags;
96 struct __kernel_ucontext* uc_link;
97 stack_t uc_stack;
98 struct sigcontext uc_mcontext;
99 __kernel_sigset_t uc_sigmask;
100} __kernel_ucontext_t;
101
102/* Second, the GLibc-compatible version */
103
104#define NGREG 19
105typedef int greg_t;
106typedef greg_t gregset_t[NGREG];
107
108enum {
109 REG_GS = 0, REG_FS, REG_ES, REG_DS,
110 REG_EDI, REG_ESI, REG_EBP, REG_ESP,
111 REG_EBX, REG_EDX, REG_ECX, REG_EAX,
112 REG_TRAPNO, REG_ERR, REG_EIP, REG_CS,
113 REG_EFL, REG_UESP, REG_SS
114};
115
116/* GLibc defines both macros and enums. Probably to let client do
117 * #ifdef REG_XXXX ... #endif. Do the same here. */
118#define REG_GS REG_GS
119#define REG_FS REG_FS
120#define REG_ES REG_ES
121#define REG_DS REG_DS
122
123#define REG_EDI REG_EDI
124#define REG_ESI REG_ESI
125#define REG_EBP REG_EBP
126#define REG_ESP REG_ESP
127
128#define REG_EBX REG_EBX
129#define REG_EDX REG_EDX
130#define REG_ECX REG_ECX
131#define REG_EAX REG_EAX
132
133#define REG_TRAPNO REG_TRAPNO
134#define REG_ERR REG_ERR
135#define REG_EIP REG_EIP
136#define REG_CS REG_CS
137#define REG_EFL REG_EFL
138#define REG_UESP REG_UESP
139#define REG_SS REG_SS
140
141/* 80-bit floating-point register */
142struct _libc_fpreg {
143 unsigned short significand[4];
144 unsigned short exponent;
145};
146
147/* Simple floating-point state, see FNSTENV instruction */
148struct _libc_fpstate {
149 unsigned long cw;
150 unsigned long sw;
151 unsigned long tag;
152 unsigned long ipoff;
153 unsigned long cssel;
154 unsigned long dataoff;
155 unsigned long datasel;
156 struct _libc_fpreg _st[8];
157 unsigned long status;
158};
159
160typedef struct _libc_fpstate* fpregset_t;
161
162typedef struct {
163 gregset_t gregs;
164 fpregset_t fpregs; /* Really a pointer to _libc_fpstate! */
165} mcontext_t;
166
167typedef struct ucontext {
168 uint32_t uc_flags;
169 struct ucontext* uc_link;
170 stack_t uc_stack;
171 mcontext_t uc_mcontext;
172 /* Only expose the 32 non-realtime signals in Bionic's 32-bit sigset_t
173 * The _unused field is required padding from the kernel. */
174 sigset_t uc_sigmask;
175 char _unused[sizeof(__kernel_sigset_t) - sizeof(sigset_t)];
176 /* Storage area for *context() functions only - private, don't use. */
177 struct _libc_fpstate __fpregs_mem;
178 /* Growth opportunity - in case we want to save MMX/SSE stuff there too */
179 int _unused2[128 - sizeof(struct _libc_fpstate)/sizeof(int)];
180} ucontext_t;
181
182__END_DECLS
183
184#endif /* _ARCH_X86_SYS_UCONTEXT_H_ */