This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 261
/
pal-arch.h
205 lines (173 loc) · 5.47 KB
/
pal-arch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/* Copyright (C) 2014 Stony Brook University */
/*
* This file contains definition of x86_64-specific aspects of PAL.
*/
#ifndef PAL_H
// TODO: fix this
#error This header is usable only inside pal.h (due to a cyclic dependency).
#endif
#ifndef PAL_ARCH_H
#define PAL_ARCH_H
#include <stdint.h>
#include "cpu.h"
#include "pal.h"
#define PAGE_SIZE (1 << 12)
#define PRESET_PAGESIZE PAGE_SIZE
typedef struct pal_tcb PAL_TCB;
#define PAL_LIBOS_TCB_SIZE 256
typedef struct pal_tcb {
struct pal_tcb* self;
/* uint64_t for alignment */
uint64_t libos_tcb[(PAL_LIBOS_TCB_SIZE + sizeof(uint64_t) - 1) / sizeof(uint64_t)];
/* data private to PAL implementation follows this struct. */
} PAL_TCB;
#include "pal_host-arch.h"
static inline PAL_TCB* pal_get_tcb(void) {
PAL_TCB* tcb;
__asm__("movq %%gs:%c1,%q0" : "=r"(tcb) : "i"(offsetof(struct pal_tcb, self)));
return tcb;
}
union pal_csgsfs {
struct {
uint16_t cs;
uint16_t gs;
uint16_t fs;
uint16_t ss;
};
uint64_t csgsfs;
};
/* Adopt Linux style fp layout, _libc_fpstate of glibc:
* Because self-contained definition is needed for Pal definition,
* same layout is defined with PAL prefix.
*/
#define PAL_FP_XSTATE_MAGIC1 0x46505853U
#define PAL_FP_XSTATE_MAGIC2 0x46505845U
#define PAL_FP_XSTATE_MAGIC2_SIZE (sizeof(PAL_FP_XSTATE_MAGIC2))
enum PAL_XFEATURE {
PAL_XFEATURE_FP,
PAL_XFEATURE_SSE,
PAL_XFEATURE_YMM,
PAL_XFEATURE_BNDREGS,
PAL_XFEATURE_BNDCSR,
PAL_XFEATURE_OPMASK,
PAL_XFEATURE_ZMM_Hi256,
PAL_XFEATURE_Hi16_ZMM,
PAL_XFEATURE_PT,
PAL_XFEATURE_PKRU,
PAL_XFEATURE_MAX,
};
#define PAL_XFEATURE_MASK_FP (1UL << PAL_XFEATURE_FP)
#define PAL_XFEATURE_MASK_SSE (1UL << PAL_XFEATURE_SSE)
#define PAL_XFEATURE_MASK_YMM (1UL << PAL_XFEATURE_YMM)
#define PAL_XFEATURE_MASK_BNDREGS (1UL << PAL_XFEATURE_BNDREGS)
#define PAL_XFEATURE_MASK_BNDCSR (1UL << PAL_XFEATURE_BNDCSR)
#define PAL_XFEATURE_MASK_OPMASK (1UL << PAL_XFEATURE_OPMASK)
#define PAL_XFEATURE_MASK_ZMM_Hi256 (1UL << PAL_XFEATURE_ZMM_Hi256)
#define PAL_XFEATURE_MASK_Hi16_ZMM (1UL << PAL_XFEATURE_Hi16_ZMM)
#define PAL_XFEATURE_MASK_PT (1UL << PAL_XFEATURE_PT)
#define PAL_XFEATURE_MASK_PKRU (1UL << PAL_XFEATURE_PKRU)
#define PAL_XFEATURE_MASK_FPSSE (PAL_XFEATURE_MASK_FP \
| PAL_XFEATURE_MASK_SSE)
#define PAL_XFEATURE_MASK_AVX512 (PAL_XFEATURE_MASK_OPMASK \
| PAL_XFEATURE_MASK_ZMM_Hi256 \
| PAL_XFEATURE_MASK_Hi16_ZMM)
typedef struct {
uint32_t magic1; /*!< PAL_FP_XSTATE_MAGIC1 */
uint32_t extended_size; /*!< g_xsave_size */
uint64_t xfeatures; /*!< XSAVE feature */
uint32_t xstate_size; /*!< g_xsave_size + PAL_FP_STATE_MAGIC2_SIZE */
uint32_t padding[7];
} PAL_FPX_SW_BYTES;
typedef struct {
uint32_t cwd;
uint32_t swd;
uint32_t twd;
uint32_t fip;
uint32_t fcs;
uint32_t foo;
uint32_t fos;
uint32_t st_space[20];
uint8_t ftop;
uint8_t changed;
uint8_t lookahead;
uint8_t no_update;
uint8_t rm;
uint8_t alimit;
void* info; /* struct math_emu_info */
uint32_t entry_eip;
} PAL_SWREGS_STATE;
typedef struct {
uint16_t significand[4];
uint16_t exponent;
uint16_t padding[3];
} PAL_FPXREG;
typedef struct {
uint32_t element[4];
} PAL_XMMREG;
typedef struct {
/* 64-bit FXSAVE format. */
uint16_t cwd;
uint16_t swd;
uint16_t ftw;
uint16_t fop;
uint64_t rip;
uint64_t rdp;
uint32_t mxcsr;
uint32_t mxcr_mask;
PAL_FPXREG st[8];
PAL_XMMREG xmm[16];
union {
uint32_t padding[24];
struct {
uint32_t padding2[12];
PAL_FPX_SW_BYTES sw_reserved;
};
};
} PAL_FPSTATE;
typedef struct {
uint64_t xfeatures;
uint64_t xcomp_bv;
uint64_t reserved[6];
} __attribute__((packed)) PAL_XSTATE_HEADER;
#define PAL_XSTATE_ALIGN 64
typedef struct {
PAL_FPSTATE fpstate;
PAL_XSTATE_HEADER header;
} __attribute__((packed, aligned(PAL_XSTATE_ALIGN))) PAL_XREGS_STATE;
/* Define PAL_CONTEXT_ outside the typedef for Doxygen */
struct PAL_CONTEXT_ {
PAL_NUM r8, r9, r10, r11, r12, r13, r14, r15;
PAL_NUM rdi, rsi, rbp, rbx, rdx, rax, rcx;
PAL_NUM rsp, rip;
PAL_NUM efl, csgsfs, err, trapno, oldmask, cr2;
PAL_XREGS_STATE* fpregs;
};
typedef struct PAL_CONTEXT_ PAL_CONTEXT;
#define DEFAULT_OBJECT_EXEC_ADDR ((void*)0x555555554000) /* Linux base location for PIE binaries */
static inline void pal_context_set_ip(PAL_CONTEXT* context, PAL_NUM insnptr) {
context->rip = insnptr;
}
static inline PAL_NUM pal_context_get_ip(PAL_CONTEXT* context) {
return context->rip;
}
static inline bool pal_context_has_user_pagefault(PAL_CONTEXT* context) {
return !!(context->err & 4);
}
/* PAL_CPU_INFO holds /proc/cpuinfo data */
typedef struct PAL_CPU_INFO_ {
/* Number of logical processors available in the host */
PAL_NUM online_logical_cores;
/* Number of physical cores in a physical package (socket) */
PAL_NUM physical_cores_per_socket;
/* array of "logical processor->physical package" mappings; has online_logical_cores elements */
int* cpu_socket;
PAL_STR cpu_vendor;
PAL_STR cpu_brand;
PAL_NUM cpu_family;
PAL_NUM cpu_model;
PAL_NUM cpu_stepping;
double cpu_bogomips;
PAL_STR cpu_flags;
} PAL_CPU_INFO;
#endif /* PAL_ARCH_H */