Loading...
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _SEGMENTATION_H
#define _SEGMENTATION_H
#include <stdint.h>
/* Host gen_idt uses this header as well, don't depend on toolchain.h */
#ifndef __packed
#define __packed __attribute__((packed))
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* Bitmask used to determine which exceptions result in an error code being
* pushed onto the stack. The following exception vectors push an error code:
*
* Vector Mnemonic Description
* ------ ------- ----------------------
* 8 #DF Double Fault
* 10 #TS Invalid TSS
* 11 #NP Segment Not Present
* 12 #SS Stack Segment Fault
* 13 #GP General Protection Fault
* 14 #PF Page Fault
* 17 #AC Alignment Check
*/
#define _EXC_ERROR_CODE_FAULTS 0x27d00
/* NOTE: We currently do not have definitions for 16-bit segment, currently
* assume everthing we are working with is 32-bit
*/
#define SEG_TYPE_LDT 0x2
#define SEG_TYPE_TASK_GATE 0x5
#define SEG_TYPE_TSS 0x9
#define SEG_TYPE_TSS_BUSY 0xB
#define SEG_TYPE_CALL_GATE 0xC
#define SEG_TYPE_IRQ_GATE 0xE
#define SEG_TYPE_TRAP_GATE 0xF
#define DT_GRAN_BYTE 0
#define DT_GRAN_PAGE 1
#define DT_READABLE 1
#define DT_NON_READABLE 0
#define DT_WRITABLE 1
#define DT_NON_WRITABLE 0
#define DT_EXPAND_DOWN 1
#define DT_EXPAND_UP 0
#define DT_CONFORM 1
#define DT_NONCONFORM 0
#define DT_TYPE_SYSTEM 0
#define DT_TYPE_CODEDATA 1
#ifndef _ASMLANGUAGE
/* Section 7.2.1 of IA architecture SW developer manual, Vol 3. */
struct __packed task_state_segment {
uint16_t backlink;
uint16_t reserved_1;
uint32_t esp0;
uint16_t ss0;
uint16_t reserved_2;
uint32_t esp1;
uint16_t ss1;
uint16_t reserved_3;
uint32_t esp2;
uint16_t ss2;
uint16_t reserved_4;
uint32_t cr3;
uint32_t eip;
uint32_t eflags;
uint32_t eax;
uint32_t ecx;
uint32_t edx;
uint32_t ebx;
uint32_t esp;
uint32_t ebp;
uint32_t esi;
uint32_t edi;
uint16_t es;
uint16_t reserved_5;
uint16_t cs;
uint16_t reserved_6;
uint16_t ss;
uint16_t reserved_7;
uint16_t ds;
uint16_t reserved_8;
uint16_t fs;
uint16_t reserved_9;
uint16_t gs;
uint16_t reserved_10;
uint16_t ldt_ss;
uint16_t reserved_11;
uint8_t t:1; /* Trap bit */
uint16_t reserved_12:15;
uint16_t iomap;
};
/* Section 3.4.2 of IA architecture SW developer manual, Vol 3. */
struct __packed segment_selector {
union {
struct {
uint8_t rpl:2;
uint8_t table:1; /* 0=gdt 1=ldt */
uint16_t index:13;
};
uint16_t val;
};
};
#define SEG_SELECTOR(index, table, dpl) (index << 3 | table << 2 | dpl)
/* References
*
* Section 5.8.3 (Call gates)
* Section 7.2.2 (TSS Descriptor)
* Section 3.4.5 (Segment descriptors)
* Section 6.11 (IDT Descriptors)
*
* IA architecture SW developer manual, Vol 3.
*/
struct __packed segment_descriptor {
/* First DWORD: 0-15 */
union {
/* IRQ, call, trap gates */
uint16_t limit_low;
/* Task gates */
uint16_t reserved_task_gate_0;
/* Everything else */
uint16_t offset_low;
};
/* First DWORD: 16-31 */
union {
/* Call/Task/Interrupt/Trap gates */
uint16_t segment_selector;
/* TSS/LDT/Segments */
uint16_t base_low; /* Bits 0-15 */
};
/* Second DWORD: 0-7 */
union {
/* TSS/LDT/Segments */
uint8_t base_mid; /* Bits 16-23 */
/* Task gates */
uint8_t reserved_task_gate_1;
/* IRQ/Trap/Call Gates */
struct {
/* Reserved except in case of call gates */
uint8_t reserved_or_param:5;
/* Bits 5-7 0 0 0 per CPU manual */
uint8_t always_0_0:3;
};
};
/* Second DWORD: 8-15 */
union {
/* Code or data Segments */
struct {
/* Set by the processor, init to 0 */
uint8_t accessed:1;
/* executable ? readable : writable */
uint8_t rw:1;
/* executable ? conforming : direction */
uint8_t cd:1;
/* 1=code 0=data */
uint8_t executable:1;
/* Next 3 fields ctually common to all */
/* 1=code or data, 0=system type */
uint8_t descriptor_type:1;
uint8_t dpl:2;
uint8_t present:1;
};
/* System types */
struct {
/* One of the SEG_TYPE_* macros above */
uint8_t type:4;
/* Alas, C doesn't let you do a union of the first
* 4 bits of a bitfield and put the rest outside of it,
* it ends up getting padded.
*/
uint8_t use_other_union:4;
};
};
/* Second DWORD: 16-31 */
union {
/* Call/IRQ/trap gates */
uint16_t offset_hi;
/* Task Gates */
uint16_t reserved_task_gate_2;
/* segment/LDT/TSS */
struct {
uint8_t limit_hi:4;
/* flags */
uint8_t avl:1; /* CPU ignores this */
/* 1=Indicates 64-bit code segment in IA-32e mode */
uint8_t flags_l:1; /* L field */
uint8_t db:1; /* D/B field 1=32-bit 0=16-bit*/
uint8_t granularity:1;
uint8_t base_hi; /* Bits 24-31 */
};
};
};
/* Address of this passed to lidt/lgdt.
* IA manual calls this a 'pseudo descriptor'.
*/
struct __packed pseudo_descriptor {
uint16_t size;
struct segment_descriptor *entries;
};
/*
* Full linear address (segment selector+offset), for far jumps/calls
*/
struct __packed far_ptr {
/** Far pointer offset, unused when invoking a task. */
void *offset;
/** Far pointer segment/gate selector. */
uint16_t sel;
};
#define DT_ZERO_ENTRY { { 0 } }
/* NOTE: the below macros only work for fixed addresses provided at build time.
* Base addresses or offsets cannot be &some_variable, as pointer values are not
* known until link time and the compiler has to split the address into various
* fields in the segment selector well before that.
*
* If you really need to put &some_variable as the base address in some
* segment descriptor, you will either need to do the assignment at runtime
* or implement some tool to populate values post-link like gen_idt does.
*/
#define _LIMIT_AND_BASE(base_p, limit_p, granularity_p) \
.base_low = (((uint32_t)base_p) & 0xFFFF), \
.base_mid = (((base_p) >> 16) & 0xFF), \
.base_hi = (((base_p) >> 24) & 0xFF), \
.limit_low = ((limit_p) & 0xFFFF), \
.limit_hi = (((limit_p) >> 16) & 0xF), \
.granularity = (granularity_p), \
.flags_l = 0, \
.db = 1, \
.avl = 0
#define _SEGMENT_AND_OFFSET(segment_p, offset_p) \
.segment_selector = (segment_p), \
.offset_low = ((offset_p) & 0xFFFF), \
.offset_hi = ((offset_p) >> 16)
#define _DESC_COMMON(dpl_p) \
.dpl = (dpl_p), \
.present = 1
#define _SYS_DESC(type_p) \
.type = type_p, \
.descriptor_type = 0
#define DT_CODE_SEG_ENTRY(base_p, limit_p, granularity_p, dpl_p, readable_p, \
conforming_p) \
{ \
_DESC_COMMON(dpl_p), \
_LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
.accessed = 0, \
.rw = (readable_p), \
.cd = (conforming_p), \
.executable = 1, \
.descriptor_type = 1 \
}
#define DT_DATA_SEG_ENTRY(base_p, limit_p, granularity_p, dpl_p, writable_p, \
direction_p) \
{ \
_DESC_COMMON(dpl_p), \
_LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
.accessed = 0, \
.rw = (writable_p), \
.cd = (direction_p), \
.executable = 0, \
.descriptor_type = 1 \
}
#define DT_LDT_ENTRY(base_p, limit_p, granularity_p, dpl_p) \
{ \
_DESC_COMMON(dpl_p), \
_LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
_SYS_DESC(SEG_TYPE_LDT) \
}
#define DT_TSS_ENTRY(base_p, limit_p, granularity_p, dpl_p) \
{ \
_DESC_COMMON(dpl_p), \
_LIMIT_AND_BASE(base_p, limit_p, granularity_p), \
_SYS_DESC(SEG_TYPE_TSS) \
}
/* "standard" TSS segments that don't stuff extra data past the end of the
* TSS struct
*/
#define DT_TSS_STD_ENTRY(base_p, dpl_p) \
DT_TSS_ENTRY(base_p, sizeof(struct task_state_segment), DT_GRAN_BYTE, \
dpl_p)
#define DT_TASK_GATE_ENTRY(segment_p, dpl_p) \
{ \
_DESC_COMMON(dpl_p), \
_SYS_DESC(SEG_TYPE_TASK_GATE), \
.segment_selector = (segment_p) \
}
#define DT_IRQ_GATE_ENTRY(segment_p, offset_p, dpl_p) \
{ \
_DESC_COMMON(dpl_p), \
_SEGMENT_AND_OFFSET(segment_p, offset_p), \
_SYS_DESC(SEG_TYPE_IRQ_GATE), \
.always_0_0 = 0 \
}
#define DT_TRAP_GATE_ENTRY(segment_p, offset_p, dpl_p) \
{ \
_DESC_COMMON(dpl_p), \
_SEGMENT_AND_OFFSET(segment_p, offset_p), \
_SYS_DESC(SEG_TYPE_TRAP_GATE), \
.always_0_0 = 0 \
}
#define DT_CALL_GATE_ENTRY(segment_p, offset_p, dpl_p, param_count_p) \
{ \
_DESC_COMMON(dpl_p), \
_SEGMENT_AND_OFFSET(segment_p, offset_p), \
_SYS_DESC(SEG_TYPE_TRAP_GATE), \
.reserved_or_param = (param_count_p), \
.always_0_0 = 0 \
}
#define DTE_BASE(dt_entry) ((dt_entry)->base_low | \
((dt_entry)->base_mid << 16) | \
((dt_entry)->base_hi << 24))
#define DTE_LIMIT(dt_entry) ((dt_entry)->limit_low | \
((dt_entry)->limit_hi << 16))
#define DTE_OFFSET(dt_entry) ((dt_entry)->offset_low | \
((dt_entry)->offset_hi << 16))
#define DT_INIT(entries) { sizeof(entries) - 1, &entries[0] }
#ifdef CONFIG_SET_GDT
/* This is either the ROM-based GDT in crt0.S or RAM-based in gdt.c,
* depending on CONFIG_GDT_DYNAMIC
*/
extern struct pseudo_descriptor _gdt;
#endif
/**
* Properly set the segment descriptor segment and offset
*
* Used for call/interrupt/trap gates
*
* @param sd Segment descriptor
* @param offset Offset within segment
* @param segment_selector Segment selector
*/
static inline void _sd_set_seg_offset(struct segment_descriptor *sd,
uint16_t segment_selector,
uint32_t offset)
{
sd->offset_low = offset & 0xFFFF;
sd->offset_hi = offset >> 16;
sd->segment_selector = segment_selector;
sd->always_0_0 = 0;
}
/**
* Initialize an segment descriptor to be a 32-bit IRQ gate
*
* @param sd Segment descriptor memory
* @param seg_selector Segment selector of handler
* @param offset offset of handler
* @param dpl descriptor privilege level
*/
static inline void _init_irq_gate(struct segment_descriptor *sd,
uint16_t seg_selector, uint32_t offset,
uint32_t dpl)
{
_sd_set_seg_offset(sd, seg_selector, offset);
sd->dpl = dpl;
sd->descriptor_type = DT_TYPE_SYSTEM;
sd->present = 1;
sd->type = SEG_TYPE_IRQ_GATE;
}
/**
* Perform an IA far jump to code within another code segment
*
* @param sel Segment selector
* @param offset Offset within that selector
*/
static inline void _far_jump(uint16_t sel, void *offset)
{
struct far_ptr ptr = {
.sel = sel,
.offset = offset
};
__asm__ __volatile__ ("ljmp *%0" :: "m" (ptr));
}
/**
* Perform an IA far call to code within another code segment
*
* @param sel Segment selector
* @param offset Offset within that selector
*/
static inline void _far_call(uint16_t sel, void *offset)
{
struct far_ptr ptr = {
.sel = sel,
.offset = offset
};
__asm__ __volatile__ ("lcall *%0" :: "m" (ptr));
}
/**
* Set current IA task TSS
*
* @param sel Segment selector in GDT for desired TSS
*/
static inline void _set_tss(uint16_t sel)
{
__asm__ __volatile__ ("ltr %0" :: "r" (sel));
}
/**
* Get the TSS segment selector in the GDT for the current IA task
*
* @return Segment selector for current IA task
*/
static inline uint16_t _get_tss(void)
{
uint16_t sel;
__asm__ __volatile__ ("str %0" : "=r" (sel));
return sel;
}
/**
* Get the current global descriptor table
*
* @param gdt Pointer to memory to receive GDT pseduo descriptor information
*/
static inline void _get_gdt(struct pseudo_descriptor *gdt)
{
__asm__ __volatile__ ("sgdt %0" : "=m" (*gdt));
}
/**
* Get the current interrupt descriptor table
*
* @param idt Pointer to memory to receive IDT pseduo descriptor information
*/
static inline void _get_idt(struct pseudo_descriptor *idt)
{
__asm__ __volatile__ ("sidt %0" : "=m" (*idt));
}
/**
* Get the current local descriptor table (LDT)
*
* @return Segment selector in the GDT for the current LDT
*/
static inline uint16_t _get_ldt(void)
{
uint16_t ret;
__asm__ __volatile__ ("sldt %0" : "=m" (ret));
return ret;
}
/**
* Set the local descriptor table for the current IA Task
*
* @param ldt Segment selector in the GDT for an LDT
*/
static inline void _set_ldt(uint16_t ldt)
{
__asm__ __volatile__ ("lldt %0" :: "m" (ldt));
}
/**
* Set the global descriptor table
*
* You will most likely need to update all the data segment registers
* and do a far call to the code segment.
*
* @param gdt Pointer to GDT pseudo descriptor.
*/
static inline void _set_gdt(const struct pseudo_descriptor *gdt)
{
__asm__ __volatile__ ("lgdt %0" :: "m" (*gdt));
}
/**
* Set the interrupt descriptor table
*
* @param idt Pointer to IDT pseudo descriptor.
*/
static inline void _set_idt(const struct pseudo_descriptor *idt)
{
__asm__ __volatile__ ("lidt %0" :: "m" (*idt));
}
/**
* Get the segment selector for the current code segment
*
* @return Segment selector
*/
static inline uint16_t _get_cs(void)
{
uint16_t cs = 0;
__asm__ __volatile__ ("mov %%cs, %0" : "=r" (cs));
return cs;
}
/**
* Get the segement selector for the current data segment
*
* @return Segment selector
*/
static inline uint16_t _get_ds(void)
{
uint16_t ds = 0;
__asm__ __volatile__ ("mov %%ds, %0" : "=r" (ds));
return ds;
}
#endif /* _ASMLANGUAGE */
#ifdef __cplusplus
}
#endif
#endif /* _SEGMENTATION_H */
|