Zephyr API Documentation 4.0.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
segmentation.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_ARCH_X86_IA32_SEGMENTATION_H_
8#define ZEPHYR_INCLUDE_ARCH_X86_IA32_SEGMENTATION_H_
9
10#include <zephyr/types.h>
11
12/* Host gen_idt uses this header as well, don't depend on toolchain.h */
13#ifndef __packed
14#define __packed __attribute__((packed))
15#endif
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/* NOTE: We currently do not have definitions for 16-bit segment, currently
22 * assume everything we are working with is 32-bit
23 */
24
25#define SEG_TYPE_LDT 0x2
26#define SEG_TYPE_TASK_GATE 0x5
27#define SEG_TYPE_TSS 0x9
28#define SEG_TYPE_TSS_BUSY 0xB
29#define SEG_TYPE_CALL_GATE 0xC
30#define SEG_TYPE_IRQ_GATE 0xE
31#define SEG_TYPE_TRAP_GATE 0xF
32
33#define DT_GRAN_BYTE 0
34#define DT_GRAN_PAGE 1
35
36#define DT_READABLE 1
37#define DT_NON_READABLE 0
38
39#define DT_WRITABLE 1
40#define DT_NON_WRITABLE 0
41
42#define DT_EXPAND_DOWN 1
43#define DT_EXPAND_UP 0
44
45#define DT_CONFORM 1
46#define DT_NONCONFORM 0
47
48#define DT_TYPE_SYSTEM 0
49#define DT_TYPE_CODEDATA 1
50
51#ifndef _ASMLANGUAGE
52
53/* Section 7.2.1 of IA architecture SW developer manual, Vol 3. */
95
96#define SEG_SELECTOR(index, table, dpl) (index << 3 | table << 2 | dpl)
97
98/* References
99 *
100 * Section 5.8.3 (Call gates)
101 * Section 7.2.2 (TSS Descriptor)
102 * Section 3.4.5 (Segment descriptors)
103 * Section 6.11 (IDT Descriptors)
104 *
105 * IA architecture SW developer manual, Vol 3.
106 */
107struct __packed segment_descriptor {
108
109 /* First DWORD: 0-15 */
110 union {
111 /* IRQ, call, trap gates */
113
114 /* Task gates */
116
117 /* Everything else */
119 };
120
121 /* First DWORD: 16-31 */
122 union {
123 /* Call/Task/Interrupt/Trap gates */
125
126 /* TSS/LDT/Segments */
127 uint16_t base_low; /* Bits 0-15 */
128 };
129
130 /* Second DWORD: 0-7 */
131 union {
132 /* TSS/LDT/Segments */
133 uint8_t base_mid; /* Bits 16-23 */
134
135 /* Task gates */
137
138 /* IRQ/Trap/Call Gates */
139 struct {
140 /* Reserved except in case of call gates */
142
143 /* Bits 5-7 0 0 0 per CPU manual */
145 };
146 };
147
148 /* Second DWORD: 8-15 */
149 union {
150 /* Code or data Segments */
151 struct {
152 /* Set by the processor, init to 0 */
154
155 /* executable ? readable : writable */
157 /* executable ? conforming : direction */
159 /* 1=code 0=data */
161
162 /* Next 3 fields actually common to all */
163
164 /* 1=code or data, 0=system type */
166
169 };
170
171 /* System types */
172 struct {
173 /* One of the SEG_TYPE_* macros above */
175
176 /* Alas, C doesn't let you do a union of the first
177 * 4 bits of a bitfield and put the rest outside of it,
178 * it ends up getting padded.
179 */
181 };
182 };
183
184 /* Second DWORD: 16-31 */
185 union {
186 /* Call/IRQ/trap gates */
188
189 /* Task Gates */
191
192 /* segment/LDT/TSS */
193 struct {
195
196 /* flags */
197 uint8_t avl:1; /* CPU ignores this */
198
199 /* 1=Indicates 64-bit code segment in IA-32e mode */
200 uint8_t flags_l:1; /* L field */
201
202 uint8_t db:1; /* D/B field 1=32-bit 0=16-bit*/
204
205 uint8_t base_hi; /* Bits 24-31 */
206 };
207 };
208
209};
210
211
212/* Address of this passed to lidt/lgdt.
213 * IA manual calls this a 'pseudo descriptor'.
214 */
219
220
221/*
222 * Full linear address (segment selector+offset), for far jumps/calls
223 */
224struct __packed far_ptr {
226 void *offset;
229};
230
231
232#define DT_ZERO_ENTRY { { 0 } }
233
234/* NOTE: the below macros only work for fixed addresses provided at build time.
235 * Base addresses or offsets cannot be &some_variable, as pointer values are not
236 * known until link time and the compiler has to split the address into various
237 * fields in the segment selector well before that.
238 *
239 * If you really need to put &some_variable as the base address in some
240 * segment descriptor, you will either need to do the assignment at runtime
241 * or implement some tool to populate values post-link like gen_idt does.
242 */
243#define _LIMIT_AND_BASE(base_p, limit_p, granularity_p) \
244 .base_low = (((uint32_t)base_p) & 0xFFFF), \
245 .base_mid = (((base_p) >> 16) & 0xFF), \
246 .base_hi = (((base_p) >> 24) & 0xFF), \
247 .limit_low = ((limit_p) & 0xFFFF), \
248 .limit_hi = (((limit_p) >> 16) & 0xF), \
249 .granularity = (granularity_p), \
250 .flags_l = 0, \
251 .db = 1, \
252 .avl = 0
253
254#define _SEGMENT_AND_OFFSET(segment_p, offset_p) \
255 .segment_selector = (segment_p), \
256 .offset_low = ((offset_p) & 0xFFFF), \
257 .offset_hi = ((offset_p) >> 16)
258
259#define _DESC_COMMON(dpl_p) \
260 .dpl = (dpl_p), \
261 .present = 1
262
263#define _SYS_DESC(type_p) \
264 .type = type_p, \
265 .descriptor_type = 0
266
267#define DT_CODE_SEG_ENTRY(base_p, limit_p, granularity_p, dpl_p, readable_p, \
268 conforming_p) \
269 { \
270 _DESC_COMMON(dpl_p), \
271 _LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
272 .accessed = 0, \
273 .rw = (readable_p), \
274 .cd = (conforming_p), \
275 .executable = 1, \
276 .descriptor_type = 1 \
277 }
278
279#define DT_DATA_SEG_ENTRY(base_p, limit_p, granularity_p, dpl_p, writable_p, \
280 direction_p) \
281 { \
282 _DESC_COMMON(dpl_p), \
283 _LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
284 .accessed = 0, \
285 .rw = (writable_p), \
286 .cd = (direction_p), \
287 .executable = 0, \
288 .descriptor_type = 1 \
289 }
290
291#define DT_LDT_ENTRY(base_p, limit_p, granularity_p, dpl_p) \
292 { \
293 _DESC_COMMON(dpl_p), \
294 _LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
295 _SYS_DESC(SEG_TYPE_LDT) \
296 }
297
298#define DT_TSS_ENTRY(base_p, limit_p, granularity_p, dpl_p) \
299 { \
300 _DESC_COMMON(dpl_p), \
301 _LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
302 _SYS_DESC(SEG_TYPE_TSS) \
303 }
304
305/* "standard" TSS segments that don't stuff extra data past the end of the
306 * TSS struct
307 */
308#define DT_TSS_STD_ENTRY(base_p, dpl_p) \
309 DT_TSS_ENTRY(base_p, sizeof(struct task_state_segment), DT_GRAN_BYTE, \
310 dpl_p)
311
312#define DT_TASK_GATE_ENTRY(segment_p, dpl_p) \
313 { \
314 _DESC_COMMON(dpl_p), \
315 _SYS_DESC(SEG_TYPE_TASK_GATE), \
316 .segment_selector = (segment_p) \
317 }
318
319#define DT_IRQ_GATE_ENTRY(segment_p, offset_p, dpl_p) \
320 { \
321 _DESC_COMMON(dpl_p), \
322 _SEGMENT_AND_OFFSET(segment_p, offset_p), \
323 _SYS_DESC(SEG_TYPE_IRQ_GATE), \
324 .always_0_0 = 0 \
325 }
326
327#define DT_TRAP_GATE_ENTRY(segment_p, offset_p, dpl_p) \
328 { \
329 _DESC_COMMON(dpl_p), \
330 _SEGMENT_AND_OFFSET(segment_p, offset_p), \
331 _SYS_DESC(SEG_TYPE_TRAP_GATE), \
332 .always_0_0 = 0 \
333 }
334
335#define DT_CALL_GATE_ENTRY(segment_p, offset_p, dpl_p, param_count_p) \
336 { \
337 _DESC_COMMON(dpl_p), \
338 _SEGMENT_AND_OFFSET(segment_p, offset_p), \
339 _SYS_DESC(SEG_TYPE_TRAP_GATE), \
340 .reserved_or_param = (param_count_p), \
341 .always_0_0 = 0 \
342 }
343
344#define DTE_BASE(dt_entry) ((dt_entry)->base_low | \
345 ((dt_entry)->base_mid << 16) | \
346 ((dt_entry)->base_hi << 24))
347
348#define DTE_LIMIT(dt_entry) ((dt_entry)->limit_low | \
349 ((dt_entry)->limit_hi << 16))
350
351#define DTE_OFFSET(dt_entry) ((dt_entry)->offset_low | \
352 ((dt_entry)->offset_hi << 16))
353
354#define DT_INIT(entries) { sizeof(entries) - 1, &entries[0] }
355
356#ifdef CONFIG_SET_GDT
357/* This is either the ROM-based GDT in crt0.S or generated by gen_gdt.py,
358 * depending on CONFIG_GDT_DYNAMIC
359 */
360extern struct pseudo_descriptor _gdt;
361#endif
362
363extern const struct pseudo_descriptor z_idt;
364
374static inline void z_sd_set_seg_offset(struct segment_descriptor *sd,
375 uint16_t segment_selector,
376 uint32_t offset)
377{
378 sd->offset_low = offset & 0xFFFFU;
379 sd->offset_hi = offset >> 16U;
380 sd->segment_selector = segment_selector;
381 sd->always_0_0 = 0U;
382}
383
384
393static inline void z_init_irq_gate(struct segment_descriptor *sd,
394 uint16_t seg_selector, uint32_t offset,
395 uint32_t dpl)
396{
397 z_sd_set_seg_offset(sd, seg_selector, offset);
398 sd->dpl = dpl;
400 sd->present = 1U;
402}
403
409static inline void _set_tss(uint16_t sel)
410{
411 __asm__ __volatile__ ("ltr %0" :: "r" (sel));
412}
413
414
420static inline uint16_t _get_tss(void)
421{
422 uint16_t sel;
423
424 __asm__ __volatile__ ("str %0" : "=r" (sel));
425 return sel;
426}
427
428
434static inline void _get_gdt(struct pseudo_descriptor *gdt)
435{
436 __asm__ __volatile__ ("sgdt %0" : "=m" (*gdt));
437}
438
439
445static inline void _get_idt(struct pseudo_descriptor *idt)
446{
447 __asm__ __volatile__ ("sidt %0" : "=m" (*idt));
448}
449
450
456static inline uint16_t _get_ldt(void)
457{
458 uint16_t ret;
459
460 __asm__ __volatile__ ("sldt %0" : "=m" (ret));
461 return ret;
462}
463
464
470static inline void _set_ldt(uint16_t ldt)
471{
472 __asm__ __volatile__ ("lldt %0" :: "m" (ldt));
473
474}
475
484static inline void _set_gdt(const struct pseudo_descriptor *gdt)
485{
486 __asm__ __volatile__ ("lgdt %0" :: "m" (*gdt));
487}
488
489
495static inline void z_set_idt(const struct pseudo_descriptor *idt)
496{
497 __asm__ __volatile__ ("lidt %0" :: "m" (*idt));
498}
499
500
506static inline uint16_t _get_cs(void)
507{
508 uint16_t cs = 0U;
509
510 __asm__ __volatile__ ("mov %%cs, %0" : "=r" (cs));
511 return cs;
512}
513
514
520static inline uint16_t _get_ds(void)
521{
522 uint16_t ds = 0U;
523
524 __asm__ __volatile__ ("mov %%ds, %0" : "=r" (ds));
525 return ds;
526}
527
528
529#endif /* _ASMLANGUAGE */
530
531#ifdef __cplusplus
532}
533#endif
534
535#endif /* ZEPHYR_INCLUDE_ARCH_X86_IA32_SEGMENTATION_H_ */
#define SEG_TYPE_IRQ_GATE
Definition segmentation.h:30
#define DT_TYPE_SYSTEM
Definition segmentation.h:48
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Definition segmentation.h:224
void * offset
Far pointer offset, unused when invoking a task.
Definition segmentation.h:226
uint16_t sel
Far pointer segment/gate selector.
Definition segmentation.h:228
Definition segmentation.h:215
uint16_t size
Definition segmentation.h:216
struct segment_descriptor * entries
Definition segmentation.h:217
Definition segmentation.h:107
uint8_t always_0_0
Definition segmentation.h:144
uint16_t reserved_task_gate_2
Definition segmentation.h:190
uint16_t base_low
Definition segmentation.h:127
uint8_t cd
Definition segmentation.h:158
uint8_t reserved_task_gate_1
Definition segmentation.h:136
uint8_t base_hi
Definition segmentation.h:205
uint8_t db
Definition segmentation.h:202
uint8_t executable
Definition segmentation.h:160
uint8_t rw
Definition segmentation.h:156
uint8_t avl
Definition segmentation.h:197
uint8_t present
Definition segmentation.h:168
uint8_t reserved_or_param
Definition segmentation.h:141
uint16_t offset_hi
Definition segmentation.h:187
uint8_t type
Definition segmentation.h:174
uint8_t descriptor_type
Definition segmentation.h:165
uint8_t base_mid
Definition segmentation.h:133
uint8_t flags_l
Definition segmentation.h:200
uint8_t accessed
Definition segmentation.h:153
uint16_t offset_low
Definition segmentation.h:118
uint16_t limit_low
Definition segmentation.h:112
uint8_t use_other_union
Definition segmentation.h:180
uint16_t reserved_task_gate_0
Definition segmentation.h:115
uint8_t limit_hi
Definition segmentation.h:194
uint16_t segment_selector
Definition segmentation.h:124
uint8_t granularity
Definition segmentation.h:203
uint8_t dpl
Definition segmentation.h:167
Definition segmentation.h:54
uint16_t ss
Definition segmentation.h:81
uint16_t reserved_9
Definition segmentation.h:86
uint16_t cs
Definition segmentation.h:79
uint16_t reserved_12
Definition segmentation.h:92
uint32_t ebp
Definition segmentation.h:74
uint32_t esp2
Definition segmentation.h:63
uint16_t ss1
Definition segmentation.h:61
uint32_t esi
Definition segmentation.h:75
uint16_t reserved_10
Definition segmentation.h:88
uint32_t edi
Definition segmentation.h:76
uint16_t reserved_4
Definition segmentation.h:65
uint32_t edx
Definition segmentation.h:71
uint32_t ecx
Definition segmentation.h:70
uint32_t cr3
Definition segmentation.h:66
uint16_t iomap
Definition segmentation.h:93
uint16_t reserved_3
Definition segmentation.h:62
uint16_t backlink
Definition segmentation.h:55
uint16_t es
Definition segmentation.h:77
uint16_t ss0
Definition segmentation.h:58
uint32_t ebx
Definition segmentation.h:72
uint16_t fs
Definition segmentation.h:85
uint16_t reserved_2
Definition segmentation.h:59
uint32_t esp1
Definition segmentation.h:60
uint32_t eip
Definition segmentation.h:67
uint16_t reserved_8
Definition segmentation.h:84
uint32_t eflags
Definition segmentation.h:68
uint16_t ldt_ss
Definition segmentation.h:89
uint16_t ds
Definition segmentation.h:83
uint16_t gs
Definition segmentation.h:87
uint16_t ss2
Definition segmentation.h:64
uint32_t eax
Definition segmentation.h:69
uint16_t reserved_5
Definition segmentation.h:78
uint16_t reserved_1
Definition segmentation.h:56
uint16_t reserved_6
Definition segmentation.h:80
uint8_t t
Definition segmentation.h:91
uint16_t reserved_7
Definition segmentation.h:82
uint32_t esp0
Definition segmentation.h:57
uint16_t reserved_11
Definition segmentation.h:90
uint32_t esp
Definition segmentation.h:73