aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/arch/i386/include/callbacks_arch.h
blob: f9cba488fcdfab83f0816000988f7c76831662a1 (plain)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* Callout/callback interface for Etherboot
 *
 * This file provides the mechanisms for making calls from Etherboot
 * to external programs and vice-versa.
 *
 * Initial version by Michael Brown <mbrown@fensystems.co.uk>, January 2004.
 *
 * $Id$
 */

#ifndef CALLBACKS_ARCH_H
#define CALLBACKS_ARCH_H

/* Skip the definitions that won't make sense to the assembler */
#ifndef ASSEMBLY

/* Struct to hold general-purpose register values.  PUSHAL and POPAL
 * can work directly with this structure; do not change the order of
 * registers.
 */
typedef struct {
	union {
		uint16_t di;
		uint32_t edi;
	};
	union {
		uint16_t si;
		uint32_t esi;
	};
	union {
		uint16_t bp;
		uint32_t ebp;
	};
	union {
		uint16_t sp;
		uint32_t esp;
	};
	union {
		struct {
			uint8_t bl;
			uint8_t bh;
		} PACKED;
		uint16_t bx;
		uint32_t ebx;
	};
	union {
		struct {
			uint8_t dl;
			uint8_t dh;
		} PACKED;
		uint16_t dx;
		uint32_t edx;
	};
	union {
		struct {
			uint8_t cl;
			uint8_t ch;
		} PACKED;
		uint16_t cx;
		uint32_t ecx;
	};
	union {
		struct {
			uint8_t al;
			uint8_t ah;
		} PACKED;
		uint16_t ax;
		uint32_t eax;
	};
} regs_t;

/* Struct to hold segment register values.  Don't change the order;
 * many bits of assembly code rely on it.
 */
typedef struct {
	uint16_t cs;
	uint16_t ss;
	uint16_t ds;
	uint16_t es;
	uint16_t fs;
	uint16_t gs;
} PACKED seg_regs_t;

/* Struct for a GDT descriptor */
typedef struct {
		uint16_t limit;
		uint32_t address;
		uint16_t padding;
} PACKED gdt_descriptor_t;

/* Struct for a GDT entry.  Use GDT_SEGMENT() to fill it in.
 */
typedef struct {
	uint16_t limit_0_15;
	uint16_t base_0_15;
	uint8_t base_16_23;
	uint8_t accessed__type__sflag__dpl__present;
	uint8_t limit_16_19__avl__size__granularity;
	uint8_t base_24_31;
} PACKED gdt_segment_t;

#define GDT_SEGMENT(base,limit,type,sflag,dpl,avl,size,granularity) \
	( (gdt_segment_t) { \
		( (limit) & 0xffff ), \
		( (base) & 0xffff ), \
		( ( (base) >> 16 ) & 0xff ), \
		( ( 1 << 0 ) | ( (type) << 1 ) | \
		  ( (sflag) << 4 ) | ( (dpl) << 5 ) | ( 1 << 7 ) ), \
		( ( (limit) >> 16 ) | \
		  ( (avl) << 4 ) | ( (size) << 5 ) | ( (granularity) << 7 ) ),\
		( (base) >> 24 ) \
	} )
#define GDT_SEGMENT_BASE(gdt_segment) \
	( (gdt_segment)->base_0_15 | \
	  (gdt_segment)->base_16_23 << 16 | \
	  (gdt_segment)->base_24_31 << 24 )
#define GDT_SEGMENT_LIMIT(gdt_segment) \
	( (gdt_segment)->limit_0_15 | \
	  ( ( (gdt_segment)->limit_16_19__avl__size__granularity \
	      & 0xf ) << 16 ) )
#define GDT_SEGMENT_GRANULARITY(gdt_segment) \
	( ( (gdt_segment)->limit_16_19__avl__size__granularity \
	    & 0x80 ) >> 7 )
#define GDT_SEGMENT_TYPE(gdt_segment) \
	( ( (gdt_segment)->accessed__type__sflag__dpl__present & 0x0e ) >> 1 )
#define GDT_SEGMENT_SIZE(gdt_segment) \
	( ( (gdt_segment)->limit_16_19__avl__size__granularity \
	    & 0x60 ) >> 5 )

#define GDT_TYPE_DATA			(0x0)
#define GDT_TYPE_STACK			(0x2)
#define GDT_TYPE_WRITEABLE		(0x1)
#define GDT_TYPE_CODE			(0x6)
#define GDT_TYPE_EXEC_ONLY_CODE		(0x4)
#define GDT_TYPE_CONFORMING		(0x1)
#define GDT_SFLAG_SYSTEM		(0)
#define GDT_SFLAG_NORMAL		(1)
#define GDT_AVL_NORMAL			(0)
#define GDT_SIZE_16BIT			(0x0)
#define GDT_SIZE_32BIT			(0x2)
#define GDT_SIZE_64BIT			(0x1)
#define GDT_SIZE_UNKNOWN		(0x3)
#define GDT_GRANULARITY_SMALL		(0)
#define GDT_GRANULARITY_LARGE		(1)
#define GDT_SEGMENT_NORMAL(base,limit,type,size,granularity) \
	GDT_SEGMENT ( base, limit, type, \
		      GDT_SFLAG_NORMAL, 0, GDT_AVL_NORMAL, \
		      size, granularity )

/* Protected mode code segment */
#define GDT_SEGMENT_PMCS(base) GDT_SEGMENT_NORMAL ( \
	base, 0xfffff, GDT_TYPE_CODE | GDT_TYPE_CONFORMING, \
	GDT_SIZE_32BIT, GDT_GRANULARITY_LARGE )
#define GDT_SEGMENT_PMCS_PHYS GDT_SEGMENT_PMCS(0)
/* Protected mode data segment */
#define GDT_SEGMENT_PMDS(base) GDT_SEGMENT_NORMAL ( \
	base, 0xfffff, GDT_TYPE_DATA | GDT_TYPE_WRITEABLE, \
	GDT_SIZE_32BIT, GDT_GRANULARITY_LARGE )
#define GDT_SEGMENT_PMDS_PHYS GDT_SEGMENT_PMDS(0)
/* Real mode code segment */
/* Not sure if there's any reason to use GDT_TYPE_EXEC_ONLY_CODE
 * instead of just GDT_TYPE_CODE, but that's what our old GDT did and
 * it worked, so I'm not changing it.
 */
#define GDT_SEGMENT_RMCS(base) GDT_SEGMENT_NORMAL ( \
	base, 0xffff, GDT_TYPE_EXEC_ONLY_CODE | GDT_TYPE_CONFORMING, \
	GDT_SIZE_16BIT, GDT_GRANULARITY_SMALL )
/* Real mode data segment */
#define GDT_SEGMENT_RMDS(base) GDT_SEGMENT_NORMAL ( \
	base, 0xffff, GDT_TYPE_DATA | GDT_TYPE_WRITEABLE, \
	GDT_SIZE_16BIT, GDT_GRANULARITY_SMALL )
/* Long mode code segment */
#define GDT_SEGMENT_LMCS(base) GDT_SEGMENT_NORMAL ( \
	base, 0xfffff, GDT_TYPE_CODE | GDT_TYPE_CONFORMING, \
	GDT_SIZE_64BIT, GDT_GRANULARITY_LARGE )
#define GDT_SEGMENT_LMCS_PHYS GDT_SEGMENT_LMCS(0)
/* Long mode data segment */
/* AFIACT, GDT_SIZE_64BIT applies only to code segments */
#define GDT_SEGMENT_LMDS(base) GDT_SEGMENT_NORMAL ( \
	base, 0xfffff, GDT_TYPE_DATA | GDT_TYPE_WRITEABLE, \
	GDT_SIZE_32BIT, GDT_GRANULARITY_LARGE )
#define GDT_SEGMENT_LMDS_PHYS GDT_SEGMENT_LMDS(0)

/* Template for creating GDT structures (including segment register
 * lists), suitable for passing as parameters to external_call().
 */
#define GDT_STRUCT_t(num_segments) \
	struct { \
		gdt_descriptor_t descriptor; \
		gdt_segment_t segments[num_segments]; \
	} PACKED
/* And utility function for filling it in */
#define GDT_ADJUST(structure) { \
	(structure)->descriptor.address = \
		virt_to_phys(&((structure)->descriptor.limit)); \
	(structure)->descriptor.limit = \
		sizeof((structure)->segments) + 8 - 1; \
	(structure)->descriptor.padding = 0; \
}

/* Data passed in to in_call() by assembly wrapper.
 */
typedef struct {
	regs_t		regs;
	seg_regs_t	seg_regs;
	gdt_descriptor_t gdt_desc;
	uint32_t	flags;
	struct {
		uint32_t offset;
		uint32_t segment;
	} ret_addr;
} PACKED i386_pm_in_call_data_t;

typedef struct {
	seg_regs_t	seg_regs;
	union {
		uint16_t	pad;
		uint16_t	prefix_sp;
	};
	uint16_t	flags;
	struct {
		uint16_t offset;
		uint16_t segment;
	} ret_addr;
	uint32_t orig_opcode;
} PACKED i386_rm_in_call_data_t;

typedef struct {
	i386_pm_in_call_data_t *pm;
	i386_rm_in_call_data_t *rm;
} i386_in_call_data_t;
#define in_call_data_t i386_in_call_data_t

/* Function prototypes
 */
extern int install_rm_callback_interface ( void *address, size_t available );

#endif /* ASSEMBLY */

#define RM_IN_CALL	(0)
#define RM_IN_CALL_FAR	(2)

#endif /* CALLBACKS_ARCH_H */