1 | .. comment SPDX-License-Identifier: CC-BY-SA-4.0 |
---|
2 | |
---|
3 | Intel/AMD x86 Specific Information |
---|
4 | ################################## |
---|
5 | |
---|
6 | This chapter discusses the Intel x86 architecture dependencies |
---|
7 | in this port of RTEMS. This family has multiple implementations |
---|
8 | from multiple vendors and suffers more from having evolved rather |
---|
9 | than being designed for growth. |
---|
10 | |
---|
11 | For information on the i386 processor, refer to the |
---|
12 | following documents: |
---|
13 | |
---|
14 | - *386 Programmer's Reference Manual, Intel, Order No. 230985-002*. |
---|
15 | |
---|
16 | - *386 Microprocessor Hardware Reference Manual, Intel, |
---|
17 | Order No. 231732-003*. |
---|
18 | |
---|
19 | - *80386 System Software Writer's Guide, Intel, Order No. 231499-001*. |
---|
20 | |
---|
21 | - *80387 Programmer's Reference Manual, Intel, Order No. 231917-001*. |
---|
22 | |
---|
23 | CPU Model Dependent Features |
---|
24 | ============================ |
---|
25 | |
---|
26 | This section presents the set of features which vary |
---|
27 | across i386 implementations and are of importance to RTEMS. |
---|
28 | The set of CPU model feature macros are defined in the file``cpukit/score/cpu/i386/i386.h`` based upon the particular CPU |
---|
29 | model specified on the compilation command line. |
---|
30 | |
---|
31 | bswap Instruction |
---|
32 | ----------------- |
---|
33 | |
---|
34 | The macro ``I386_HAS_BSWAP`` is set to 1 to indicate that |
---|
35 | this CPU model has the ``bswap`` instruction which |
---|
36 | endian swaps a thirty-two bit quantity. This instruction |
---|
37 | appears to be present in all CPU models |
---|
38 | i486's and above. |
---|
39 | |
---|
40 | Calling Conventions |
---|
41 | =================== |
---|
42 | |
---|
43 | Processor Background |
---|
44 | -------------------- |
---|
45 | |
---|
46 | The i386 architecture supports a simple yet effective |
---|
47 | call and return mechanism. A subroutine is invoked via the call |
---|
48 | (``call``) instruction. This instruction pushes the return address |
---|
49 | on the stack. The return from subroutine (``ret``) instruction pops |
---|
50 | the return address off the current stack and transfers control |
---|
51 | to that instruction. It is is important to note that the i386 |
---|
52 | call and return mechanism does not automatically save or restore |
---|
53 | any registers. It is the responsibility of the high-level |
---|
54 | language compiler to define the register preservation and usage |
---|
55 | convention. |
---|
56 | |
---|
57 | Calling Mechanism |
---|
58 | ----------------- |
---|
59 | |
---|
60 | All RTEMS directives are invoked using a call instruction and return to |
---|
61 | the user application via the ret instruction. |
---|
62 | |
---|
63 | Register Usage |
---|
64 | -------------- |
---|
65 | |
---|
66 | As discussed above, the call instruction does not automatically save |
---|
67 | any registers. RTEMS uses the registers EAX, ECX, and EDX as scratch |
---|
68 | registers. These registers are not preserved by RTEMS directives |
---|
69 | therefore, the contents of these registers should not be assumed upon |
---|
70 | return from any RTEMS directive. |
---|
71 | |
---|
72 | Parameter Passing |
---|
73 | ----------------- |
---|
74 | |
---|
75 | RTEMS assumes that arguments are placed on the |
---|
76 | current stack before the directive is invoked via the call |
---|
77 | instruction. The first argument is assumed to be closest to the |
---|
78 | return address on the stack. This means that the first argument |
---|
79 | of the C calling sequence is pushed last. The following |
---|
80 | pseudo-code illustrates the typical sequence used to call a |
---|
81 | RTEMS directive with three (3) arguments: |
---|
82 | .. code:: c |
---|
83 | |
---|
84 | push third argument |
---|
85 | push second argument |
---|
86 | push first argument |
---|
87 | invoke directive |
---|
88 | remove arguments from the stack |
---|
89 | |
---|
90 | The arguments to RTEMS are typically pushed onto the |
---|
91 | stack using a push instruction. These arguments must be removed |
---|
92 | from the stack after control is returned to the caller. This |
---|
93 | removal is typically accomplished by adding the size of the |
---|
94 | argument list in bytes to the stack pointer. |
---|
95 | |
---|
96 | Memory Model |
---|
97 | ============ |
---|
98 | |
---|
99 | Flat Memory Model |
---|
100 | ----------------- |
---|
101 | |
---|
102 | RTEMS supports the i386 protected mode, flat memory |
---|
103 | model with paging disabled. In this mode, the i386 |
---|
104 | automatically converts every address from a logical to a |
---|
105 | physical address each time it is used. The i386 uses |
---|
106 | information provided in the segment registers and the Global |
---|
107 | Descriptor Table to convert these addresses. RTEMS assumes the |
---|
108 | existence of the following segments: |
---|
109 | |
---|
110 | - a single code segment at protection level (0) which |
---|
111 | contains all application and executive code. |
---|
112 | |
---|
113 | - a single data segment at protection level zero (0) which |
---|
114 | contains all application and executive data. |
---|
115 | |
---|
116 | The i386 segment registers and associated selectors |
---|
117 | must be initialized when the initialize_executive directive is |
---|
118 | invoked. RTEMS treats the segment registers as system registers |
---|
119 | and does not modify or context switch them. |
---|
120 | |
---|
121 | This i386 memory model supports a flat 32-bit address |
---|
122 | space with addresses ranging from 0x00000000 to 0xFFFFFFFF (4 |
---|
123 | gigabytes). Each address is represented by a 32-bit value and |
---|
124 | is byte addressable. The address may be used to reference a |
---|
125 | single byte, half-word (2-bytes), or word (4 bytes). |
---|
126 | |
---|
127 | Interrupt Processing |
---|
128 | ==================== |
---|
129 | |
---|
130 | Although RTEMS hides many of the processor |
---|
131 | dependent details of interrupt processing, it is important to |
---|
132 | understand how the RTEMS interrupt manager is mapped onto the |
---|
133 | processor's unique architecture. Discussed in this chapter are |
---|
134 | the the processor's response and control mechanisms as they |
---|
135 | pertain to RTEMS. |
---|
136 | |
---|
137 | Vectoring of Interrupt Handler |
---|
138 | ------------------------------ |
---|
139 | |
---|
140 | Although the i386 supports multiple privilege levels, |
---|
141 | RTEMS and all user software executes at privilege level 0. This |
---|
142 | decision was made by the RTEMS designers to enhance |
---|
143 | compatibility with processors which do not provide sophisticated |
---|
144 | protection facilities like those of the i386. This decision |
---|
145 | greatly simplifies the discussion of i386 processing, as one |
---|
146 | need only consider interrupts without privilege transitions. |
---|
147 | |
---|
148 | Upon receipt of an interrupt the i386 automatically |
---|
149 | performs the following actions: |
---|
150 | |
---|
151 | - pushes the EFLAGS register |
---|
152 | |
---|
153 | - pushes the far address of the interrupted instruction |
---|
154 | |
---|
155 | - vectors to the interrupt service routine (ISR). |
---|
156 | |
---|
157 | A nested interrupt is processed similarly by the |
---|
158 | i386. |
---|
159 | |
---|
160 | Interrupt Stack Frame |
---|
161 | --------------------- |
---|
162 | |
---|
163 | The structure of the Interrupt Stack Frame for the |
---|
164 | i386 which is placed on the interrupt stack by the processor in |
---|
165 | response to an interrupt is as follows: |
---|
166 | |
---|
167 | +----------------------+-------+ |
---|
168 | | Old EFLAGS Register | ESP+8 | |
---|
169 | +----------+-----------+-------+ |
---|
170 | | UNUSED | Old CS | ESP+4 | |
---|
171 | +----------+-----------+-------+ |
---|
172 | | Old EIP | ESP | |
---|
173 | +----------------------+-------+ |
---|
174 | |
---|
175 | |
---|
176 | Interrupt Levels |
---|
177 | ---------------- |
---|
178 | |
---|
179 | Although RTEMS supports 256 interrupt levels, the |
---|
180 | i386 only supports two - enabled and disabled. Interrupts are |
---|
181 | enabled when the interrupt-enable flag (IF) in the extended |
---|
182 | flags (EFLAGS) is set. Conversely, interrupt processing is |
---|
183 | inhibited when the IF is cleared. During a non-maskable |
---|
184 | interrupt, all other interrupts, including other non-maskable |
---|
185 | ones, are inhibited. |
---|
186 | |
---|
187 | RTEMS interrupt levels 0 and 1 such that level zero |
---|
188 | (0) indicates that interrupts are fully enabled and level one |
---|
189 | that interrupts are disabled. All other RTEMS interrupt levels |
---|
190 | are undefined and their behavior is unpredictable. |
---|
191 | |
---|
192 | Interrupt Stack |
---|
193 | --------------- |
---|
194 | |
---|
195 | The i386 family does not support a dedicated hardware |
---|
196 | interrupt stack. On this processor, RTEMS allocates and manages |
---|
197 | a dedicated interrupt stack. As part of vectoring a non-nested |
---|
198 | interrupt service routine, RTEMS switches from the stack of the |
---|
199 | interrupted task to a dedicated interrupt stack. When a |
---|
200 | non-nested interrupt returns, RTEMS switches back to the stack |
---|
201 | of the interrupted stack. The current stack pointer is not |
---|
202 | altered by RTEMS on nested interrupt. |
---|
203 | |
---|
204 | Default Fatal Error Processing |
---|
205 | ============================== |
---|
206 | |
---|
207 | The default fatal error handler for this architecture disables processor |
---|
208 | interrupts, places the error code in EAX, and executes a HLT instruction |
---|
209 | to halt the processor. |
---|
210 | |
---|
211 | Symmetric Multiprocessing |
---|
212 | ========================= |
---|
213 | |
---|
214 | SMP is not supported. |
---|
215 | |
---|
216 | Thread-Local Storage |
---|
217 | ==================== |
---|
218 | |
---|
219 | Thread-local storage is not implemented. |
---|
220 | |
---|
221 | Board Support Packages |
---|
222 | ====================== |
---|
223 | |
---|
224 | System Reset |
---|
225 | ------------ |
---|
226 | |
---|
227 | An RTEMS based application is initiated when the i386 processor is reset. |
---|
228 | When the i386 is reset, |
---|
229 | |
---|
230 | - The EAX register is set to indicate the results of the processor's |
---|
231 | power-up self test. If the self-test was not executed, the contents of |
---|
232 | this register are undefined. Otherwise, a non-zero value indicates the |
---|
233 | processor is faulty and a zero value indicates a successful self-test. |
---|
234 | |
---|
235 | - The DX register holds a component identifier and revision level. DH |
---|
236 | contains 3 to indicate an i386 component and DL contains a unique revision |
---|
237 | level indicator. |
---|
238 | |
---|
239 | - Control register zero (CR0) is set such that the processor is in real |
---|
240 | mode with paging disabled. Other portions of CR0 are used to indicate the |
---|
241 | presence of a numeric coprocessor. |
---|
242 | |
---|
243 | - All bits in the extended flags register (EFLAG) which are not |
---|
244 | permanently set are cleared. This inhibits all maskable interrupts. |
---|
245 | |
---|
246 | - The Interrupt Descriptor Register (IDTR) is set to point at address |
---|
247 | zero. |
---|
248 | |
---|
249 | - All segment registers are set to zero. |
---|
250 | |
---|
251 | - The instruction pointer is set to 0x0000FFF0. The first instruction |
---|
252 | executed after a reset is actually at 0xFFFFFFF0 because the i386 asserts |
---|
253 | the upper twelve address until the first intersegment (FAR) JMP or CALL |
---|
254 | instruction. When a JMP or CALL is executed, the upper twelve address |
---|
255 | lines are lowered and the processor begins executing in the first megabyte |
---|
256 | of memory. |
---|
257 | |
---|
258 | Typically, an intersegment JMP to the application's initialization code is |
---|
259 | placed at address 0xFFFFFFF0. |
---|
260 | |
---|
261 | Processor Initialization |
---|
262 | ------------------------ |
---|
263 | |
---|
264 | This initialization code is responsible for initializing all data |
---|
265 | structures required by the i386 in protected mode and for actually entering |
---|
266 | protected mode. The i386 must be placed in protected mode and the segment |
---|
267 | registers and associated selectors must be initialized before the |
---|
268 | initialize_executive directive is invoked. |
---|
269 | |
---|
270 | The initialization code is responsible for initializing the Global |
---|
271 | Descriptor Table such that the i386 is in the thirty-two bit flat memory |
---|
272 | model with paging disabled. In this mode, the i386 automatically converts |
---|
273 | every address from a logical to a physical address each time it is used. |
---|
274 | For more information on the memory model used by RTEMS, please refer to the |
---|
275 | Memory Model chapter in this document. |
---|
276 | |
---|
277 | Since the processor is in real mode upon reset, the processor must be |
---|
278 | switched to protected mode before RTEMS can execute. Before switching to |
---|
279 | protected mode, at least one descriptor table and two descriptors must be |
---|
280 | created. Descriptors are needed for a code segment and a data segment. ( |
---|
281 | This will give you the flat memory model.) The stack can be placed in a |
---|
282 | normal read/write data segment, so no descriptor for the stack is needed. |
---|
283 | Before the GDT can be used, the base address and limit must be loaded into |
---|
284 | the GDTR register using an LGDT instruction. |
---|
285 | |
---|
286 | If the hardware allows an NMI to be generated, you need to create the IDT |
---|
287 | and a gate for the NMI interrupt handler. Before the IDT can be used, the |
---|
288 | base address and limit for the idt must be loaded into the IDTR register |
---|
289 | using an LIDT instruction. |
---|
290 | |
---|
291 | Protected mode is entered by setting thye PE bit in the CR0 register. |
---|
292 | Either a LMSW or MOV CR0 instruction may be used to set this bit. Because |
---|
293 | the processor overlaps the interpretation of several instructions, it is |
---|
294 | necessary to discard the instructions from the read-ahead cache. A JMP |
---|
295 | instruction immediately after the LMSW changes the flow and empties the |
---|
296 | processor if intructions which have been pre-fetched and/or decoded. At |
---|
297 | this point, the processor is in protected mode and begins to perform |
---|
298 | protected mode application initialization. |
---|
299 | |
---|
300 | If the application requires that the IDTR be some value besides zero, then |
---|
301 | it should set it to the required value at this point. All tasks share the |
---|
302 | same i386 IDTR value. Because interrupts are enabled automatically by |
---|
303 | RTEMS as part of the initialize_executive directive, the IDTR MUST be set |
---|
304 | properly before this directive is invoked to insure correct interrupt |
---|
305 | vectoring. If processor caching is to be utilized, then it should be |
---|
306 | enabled during the reset application initialization code. The reset code |
---|
307 | which is executed before the call to initialize_executive has the following |
---|
308 | requirements: |
---|
309 | |
---|
310 | For more information regarding the i386 data structures and their |
---|
311 | contents, refer to Intel's 386 Programmer's Reference Manual. |
---|
312 | |
---|
313 | .. COMMENT: COPYRIGHT (c) 1988-2002. |
---|
314 | |
---|
315 | .. COMMENT: On-Line Applications Research Corporation (OAR). |
---|
316 | |
---|
317 | .. COMMENT: All rights reserved. |
---|
318 | |
---|
319 | .. COMMENT: Jukka Pietarinen <jukka.pietarinen@mrf.fi>, 2008, |
---|
320 | |
---|
321 | .. COMMENT: Micro-Research Finland Oy |
---|
322 | |
---|