Contiki 3.x
gdt-layout.h
1 /*
2  * Copyright (C) 2015-2016, Intel Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef CPU_X86_MM_GDT_LAYOUT_H_
32 #define CPU_X86_MM_GDT_LAYOUT_H_
33 
34 #include "prot-domains.h"
35 
36 #if X86_CONF_PROT_DOMAINS == X86_CONF_PROT_DOMAINS__PAGING
37 /**
38  * Number of fixed GDT descriptors. Additional descriptors may be defined
39  * outside of gdt.c.
40  */
41 #define GDT_NUM_FIXED_DESC 7
42 #elif X86_CONF_PROT_DOMAINS_MULTI_SEG
43 #define GDT_NUM_FIXED_DESC 11
44 #else
45 #define GDT_NUM_FIXED_DESC 3
46 #endif
47 
48 #define GDT_IDX_NULL 0
49 /**
50  * Flat code segment, used at boot and also for the rest of the system's
51  * runtime when protection domains are disabled
52  */
53 #define GDT_IDX_CODE_FLAT 1
54 /**
55  * Flat data segment, used at boot and also for the rest of the system's
56  * runtime when protection domains are disabled
57  */
58 #define GDT_IDX_DATA_FLAT 2
59 
60 #if X86_CONF_PROT_DOMAINS != X86_CONF_PROT_DOMAINS__NONE
61 /** Default (post-boot) code segment */
62 #define GDT_IDX_CODE 3
63 /**
64  * Same bounds and permissions as default code segment, but at the interrupt
65  * handler privilege level
66  */
67 #define GDT_IDX_CODE_INT 4
68 /** Stack segment for interrupt handlers */
69 #define GDT_IDX_STK_INT 5
70 
71 #if X86_CONF_PROT_DOMAINS == X86_CONF_PROT_DOMAINS__PAGING
72 #define GDT_IDX_CODE_EXC GDT_IDX_CODE_FLAT
73 /** Default data segment used by code at all privilege levels */
74 #define GDT_IDX_DATA 6
75 #define GDT_IDX_STK GDT_IDX_DATA
76 #define GDT_IDX_STK_EXC GDT_IDX_DATA_FLAT
77 #else
78 /**
79  * Same bounds and permissions as default code segment, but at the exception
80  * handler privilege level
81  */
82 #define GDT_IDX_CODE_EXC 6
83 /** R/W kernel data descriptor used during boot stage 1 */
84 #define GDT_IDX_DATA_KERN_EXC 7
85 /** Default data segment used by code at all privilege levels */
86 #define GDT_IDX_DATA 8
87 /**
88  * Default stack segment, which overlaps with the beginning of the default data
89  * segment
90  */
91 #define GDT_IDX_STK 9
92 /** Stack segment for exception handlers */
93 #define GDT_IDX_STK_EXC 10
94 
95 #if X86_CONF_PROT_DOMAINS == X86_CONF_PROT_DOMAINS__TSS
96 #define GDT_IDX_TSS(dom_id) (GDT_NUM_FIXED_DESC + (2 * (dom_id)))
97 #define GDT_IDX_LDT(dom_id) (GDT_NUM_FIXED_DESC + (2 * (dom_id)) + 1)
98 #else
99 #define GDT_IDX_LDT(dom_id) (GDT_NUM_FIXED_DESC + (dom_id))
100 #endif
101 
102 #endif
103 #else
104 #define GDT_IDX_CODE GDT_IDX_CODE_FLAT
105 #define GDT_IDX_CODE_INT GDT_IDX_CODE_FLAT
106 #define GDT_IDX_CODE_EXC GDT_IDX_CODE_FLAT
107 #define GDT_IDX_DATA GDT_IDX_DATA_FLAT
108 #define GDT_IDX_STK GDT_IDX_DATA_FLAT
109 #define GDT_IDX_STK_INT GDT_IDX_DATA_FLAT
110 #define GDT_IDX_STK_EXC GDT_IDX_DATA_FLAT
111 #endif
112 
113 #define GDT_SEL(idx, rpl) (((idx) << 3) | (rpl))
114 
115 #define DT_SEL_GET_IDX(sel) ((sel) >> 3)
116 
117 #define DT_SEL_GET_RPL(sel) ((sel) & 3)
118 
119 #define GDT_SEL_NULL GDT_SEL(GDT_IDX_NULL, 0)
120 #define GDT_SEL_CODE_FLAT GDT_SEL(GDT_IDX_CODE_FLAT, PRIV_LVL_EXC)
121 #define GDT_SEL_DATA_FLAT GDT_SEL(GDT_IDX_DATA_FLAT, PRIV_LVL_EXC)
122 
123 #define GDT_SEL_CODE GDT_SEL(GDT_IDX_CODE, PRIV_LVL_USER)
124 #define GDT_SEL_CODE_INT GDT_SEL(GDT_IDX_CODE_INT, PRIV_LVL_INT)
125 #define GDT_SEL_CODE_EXC GDT_SEL(GDT_IDX_CODE_EXC, PRIV_LVL_EXC)
126 
127 #define GDT_SEL_DATA GDT_SEL(GDT_IDX_DATA, PRIV_LVL_EXC)
128 #define GDT_SEL_DATA_KERN_EXC GDT_SEL(GDT_IDX_DATA_KERN_EXC, PRIV_LVL_EXC)
129 
130 #define GDT_SEL_STK GDT_SEL(GDT_IDX_STK, PRIV_LVL_USER)
131 #define GDT_SEL_STK_INT GDT_SEL(GDT_IDX_STK_INT, PRIV_LVL_INT)
132 #define GDT_SEL_STK_EXC GDT_SEL(GDT_IDX_STK_EXC, PRIV_LVL_EXC)
133 
134 #define GDT_SEL_TSS(dom_id) GDT_SEL(GDT_IDX_TSS(dom_id), PRIV_LVL_USER)
135 #define GDT_SEL_LDT(dom_id) GDT_SEL(GDT_IDX_LDT(dom_id), PRIV_LVL_USER)
136 
137 #endif /* CPU_X86_MM_GDT_LAYOUT_H_ */
138