summaryrefslogtreecommitdiff
path: root/core/minute-ia/ia_structs.h
blob: 29bbb6c0055c2ee65c0d77ab7ec3ea96b44b4518 (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
/* Copyright 2019 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef __CROS_EC_IA_STRUCTS_H
#define __CROS_EC_IA_STRUCTS_H

#ifndef __ASSEMBLER__

#include "common.h"


/**
 * IA32/x86 architecture related data structure definitions.
 * including: Global Descriptor Table (GDT), Local Descriptor Table (LDT),
 * Interrupt Descriptor Table (IDT) and Task State Segment (TSS)
 * see: https://en.wikipedia.org/wiki/Global_Descriptor_Table
 *      https://en.wikipedia.org/wiki/Interrupt_descriptor_table
 *      https://en.wikipedia.org/wiki/Task_state_segment
 */

/* GDT entry descriptor */
struct gdt_entry {
	union {
		struct {
			uint32_t dword_lo;	/* lower dword */
			uint32_t dword_up;	/* upper dword */
		};
		struct {
			uint16_t limit_lw;	/* limit (0:15) */
			uint16_t base_addr_lw;	/* base address (0:15) */
			uint8_t base_addr_mb;	/* base address (16:23) */
			uint8_t flags;		/* flags */
			uint8_t limit_ub;	/* limit (16:19) */
			uint8_t base_addr_ub;	/* base address (24:31) */
		};
	};

} __packed;

typedef struct gdt_entry ldt_entry;

/* GDT header */
struct gdt_header {
	uint16_t limit;			/* GDT limit size */
	struct gdt_entry *entries;	/* pointer to GDT entries */
} __packed;

/* IDT entry descriptor */
struct idt_entry {
	union {
		struct {
			uint32_t dword_lo;	/* lower dword */
			uint32_t dword_up;	/* upper dword */
		};

		struct {
			uint16_t offset_lw;	/* offset (0:15) */
			uint16_t seg_selector;	/* segment selector */
			uint8_t zero;		/* must be set to zero */
			uint8_t flags;		/* flags */
			uint16_t offset_uw;	/* offset (16:31) */
		};
	};
} __packed;

/* IDT header */
struct idt_header {
	uint16_t limit;			/* IDT limit size */
	struct idt_entry *entries;	/* pointer to IDT entries */
} __packed;

/* TSS entry descriptor */
struct tss_entry {
	uint16_t prev_task_link;
	uint16_t reserved1;
	uint8_t *esp0;
	uint16_t ss0;
	uint16_t reserved2;
	uint8_t *esp1;
	uint16_t ss1;
	int16_t reserved3;
	uint8_t *esp2;
	uint16_t ss2;
	uint16_t reserved4;
	uint32_t cr3;
	int32_t eip;
	uint32_t eflags;
	uint32_t eax;
	uint32_t ecx;
	int32_t edx;
	uint32_t ebx;
	uint32_t esp;
	uint32_t ebp;
	uint32_t esi;
	uint32_t edi;
	int16_t es;
	uint16_t reserved5;
	uint16_t cs;
	uint16_t reserved6;
	uint16_t ss;
	uint16_t reserved7;
	uint16_t ds;
	uint16_t reserved8;
	uint16_t fs;
	uint16_t reserved9;
	uint16_t gs;
	uint16_t reserved10;
	uint16_t ldt_seg_selector;
	uint16_t reserved11;
	uint16_t trap_debug;
	/* offset from TSS base for I/O perms */
	uint16_t iomap_base_addr;
} __packed;

#endif

/* code segment flag,  E/R, Present = 1, DPL = 0, Acesssed = 1 */
#define GDT_DESC_CODE_FLAGS	(0x9B)

/* data segment flag,  R/W, Present = 1, DPL = 0, Acesssed = 1 */
#define GDT_DESC_DATA_FLAGS	(0x93)

/* TSS segment limit size */
#define GDT_DESC_TSS_LIMIT	(0x67)

/* TSS segment flag, Present = 1, DPL = 0, Acesssed = 1 */
#define GDT_DESC_TSS_FLAGS	(0x89)

/* LDT segment flag, Present = 1, DPL = 0 */
#define GDT_DESC_LDT_FLAGS	(0x82)

/* IDT descriptor flag, Present = 1, DPL = 0, 32-bit interrupt gate */
#define IDT_DESC_FLAGS		(0x8E)

/**
 * macros helper to create a GDT entry descriptor
 * set default 4096-byte pages for granularity
 * base: 32bit base address
 * limit: 32bit limit size of bytes (will covert to unit of 4096-byte pages)
 * flags: 8bit flags
 */
#define GEN_GDT_DESC_LO(base, limit, flags)                                    \
	((((limit) >> 12) & 0xFFFF) | (((base) & 0xFFFF) << 16))

#define GEN_GDT_DESC_UP(base, limit, flags)                                    \
	((((base) >> 16) & 0xFF) | (((flags) << 8) & 0xFF00) |                 \
	(((limit) >> 12) & 0xFF0000) | ((base) & 0xFF000000) | 0xc00000)


/**
 * macro helper to create a IDT entry descriptor
 */
#define GEN_IDT_DESC_LO(offset, selector, flags)                               \
	(((uint32_t)(offset) & 0xFFFF) | (((selector) & 0xFFFF) << 16))

#define GEN_IDT_DESC_UP(offset, selector, flags)                               \
	(((uint32_t)(offset) & 0xFFFF0000) | (((flags) & 0xFF) << 8))

#endif /* __CROS_EC_IA_STRUCTS_H */