Contiki 3.x
segmentation.h
1 /*
2  * Copyright (C) 2015, 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_SEGMENTATION_H_
32 #define CPU_X86_MM_SEGMENTATION_H_
33 
34 #include <stdint.h>
35 
36 #define SEG_FLAG(lbl, val) \
37  (((val) & (~0u >> (32 - SEG_WIDTH_##lbl))) << SEG_SHAMT_##lbl)
38 
39 #define SEG_SET_FLAG(desc, lbl, val) \
40  (desc).flags = ((desc).flags & ~SEG_FLAG(lbl, ~0u)) | SEG_FLAG(lbl, val)
41 
42 #define SEG_WIDTH_TYPE 4
43 #define SEG_SHAMT_TYPE 0
44 #define SEG_WIDTH_DESCTYPE 1
45 #define SEG_SHAMT_DESCTYPE 4
46 #define SEG_WIDTH_DPL 2
47 #define SEG_SHAMT_DPL 5
48 #define SEG_WIDTH_PRESENT 1
49 #define SEG_SHAMT_PRESENT 7
50 #define SEG_WIDTH_LIMIT_HI 4
51 #define SEG_SHAMT_LIMIT_HI 8
52 #define SEG_WIDTH_AVL 1
53 #define SEG_SHAMT_AVL 12
54 #define SEG_WIDTH_LONG_MODE 1
55 #define SEG_SHAMT_LONG_MODE 13
56 /* also used to indicate default operand and address size */
57 #define SEG_WIDTH_DIRECTION 1
58 #define SEG_SHAMT_DIRECTION 14
59 #define SEG_WIDTH_GRAN 1
60 #define SEG_SHAMT_GRAN 15
61 
62 #define SEG_TYPE_DATA_RDONLY SEG_FLAG(TYPE, 0x00) /* Read only */
63 #define SEG_TYPE_DATA_RDWR SEG_FLAG(TYPE, 0x02) /* Read/Write */
64 #define SEG_TYPE_CODE_EXRD SEG_FLAG(TYPE, 0x0A) /* Execute/Read */
65 #define SEG_TYPE_CODE_EX SEG_FLAG(TYPE, 0x08) /* Execute only */
66 #define SEG_TYPE_LDT SEG_FLAG(TYPE, 0x02)
67 #define SEG_TYPE_TSS32_AVAIL SEG_FLAG(TYPE, 0x09)
68 
69 #define SEG_DESCTYPE_SYS SEG_FLAG(DESCTYPE, 0)
70 #define SEG_DESCTYPE_NSYS SEG_FLAG(DESCTYPE, 1)
71 
72 #define SEG_PRESENT SEG_FLAG(PRESENT, 1)
73 
74 #define SEG_DEFL_OPSZ_32BIT SEG_FLAG(DIRECTION, 1)
75 
76 #define SEG_GRAN_BYTE SEG_FLAG(GRAN, 0)
77 #define SEG_GRAN_PAGE SEG_FLAG(GRAN, 1)
78 
79 /**
80  * Maximum length of segment that can be regulated with a byte-granularity
81  * segment limit.
82  */
83 #define SEG_MAX_BYTE_GRAN_LEN (1 << 20)
84 
85 /**
86  * Segment descriptor. See Intel Combined Manual,
87  * Vol. 3, Section 3.4.5 for more details.
88  */
89 typedef union segment_desc {
90  struct {
91  uint32_t lim_lo : 16;
92  uint32_t base_lo : 16;
93  uint32_t base_mid : 8;
94  uint32_t flags : 16;
95  uint32_t base_hi : 8;
96  };
97  struct {
98  uint32_t raw_lo, raw_hi;
99  };
100  uint64_t raw;
102 
103 #define SEG_DESC_NOT_PRESENT 0
104 
105 /* The next two functions are invoked by boot code, so they must always be
106  * inlined to avoid being placed in a different address space than the initial,
107  * flat address space.
108  */
109 static inline void __attribute__((always_inline))
110 segment_desc_set_limit(segment_desc_t *c_this, uint32_t len)
111 {
112  uint32_t limit = len - 1;
113 
114  SEG_SET_FLAG(*c_this, LIMIT_HI, limit >> 16); /* set limit bits 19:16 */
115  c_this->lim_lo = limit; /* set limit bits 15:0 */
116 }
117 /**
118  * \brief Initialize a segment descriptor.
119  * \param c_this Segment descriptor to be initialized.
120  * \param base Base address of region to be covered by segment descriptor.
121  * \param len Length to be specified by segment descriptor. The units may
122  * be bytes or pages, depending on the flags.
123  * \param flags Flags to be added to the default flags: present, default
124  * operand size of 32 bits, and high limit bits.
125  */
126 static inline void __attribute__((always_inline))
127 segment_desc_init(segment_desc_t *c_this,
128  uint32_t base, uint32_t len, uint16_t flags)
129 {
130  c_this->raw = 0;
131 
132  /* Create the high 32 bit segment */
133  c_this->base_mid = base >> 16; /* set base bits 23:16 */
134  c_this->base_hi = base >> 24; /* set base bits 31:24 */
135 
136  /* Create the low 32 bit segment */
137  c_this->base_lo = base; /* set base bits 15:0 */
138 
139  c_this->flags = SEG_FLAG(PRESENT, 1) | SEG_DEFL_OPSZ_32BIT | flags;
140 
141  /* This must be done after setting the other flags, or else it
142  * would be partially overridden.
143  */
144  segment_desc_set_limit(c_this, len);
145 }
146 #endif /* CPU_X86_MM_SEGMENTATION_H_ */
#define __attribute__(nothing)
Define attribute to nothing since it isn't handled by IAR.
Definition: iar.h:194
Segment descriptor.
Definition: segmentation.h:89