summaryrefslogtreecommitdiffstats
path: root/c/src/exec/score/cpu/i386/i386.h
blob: 1832d4142e039efe9624f96deb428005640c994b (plain) (blame)
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
/*  i386.h
 *
 *  This include file contains information pertaining to the Intel
 *  i386 processor.
 *
 *  COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994.
 *  On-Line Applications Research Corporation (OAR).
 *  All rights assigned to U.S. Government, 1994.
 *
 *  This material may be reproduced by or for the U.S. Government pursuant
 *  to the copyright license under the clause at DFARS 252.227-7013.  This
 *  notice must appear in all copies of this file and its derivatives.
 *
 *  $Id$
 */

#ifndef __i386_h
#define __i386_h

#ifdef __cplusplus
extern "C" {
#endif

/*
 *  The following define the CPU Family and Model within the family
 *
 *  NOTE: The string "REPLACE_THIS_WITH_THE_CPU_MODEL" is replaced
 *        with the name of the appropriate macro for this target CPU.
 */

#ifdef i386
#undef i386
#endif
#define i386 1

#ifdef REPLACE_THIS_WITH_THE_CPU_MODEL
#undef REPLACE_THIS_WITH_THE_CPU_MODEL
#endif
#define REPLACE_THIS_WITH_THE_CPU_MODEL

#ifdef REPLACE_THIS_WITH_THE_BSP
#undef REPLACE_THIS_WITH_THE_BSP
#endif
#define REPLACE_THIS_WITH_THE_BSP

/*
 *  This section contains the information required to build
 *  RTEMS for a particular member of the Intel i386
 *  family when executing in protected mode.  It does
 *  this by setting variables to indicate which implementation
 *  dependent features are present in a particular member
 *  of the family.
 *
 *  Currently recognized:
 *    i386_fp    (i386 DX or SX w/i387)
 *    i386_fp    (i386 DX or SX w/o i387)
 *    i486dx
 *    i486sx
 *    pentium
 *
 *  Floating point is the only feature which currently varies.  Eventually
 *  the i486-plus level instruction for endian swapping should be added
 *  to this feature list.
 */

#if defined(i386_fp)

#define RTEMS_MODEL_NAME  "i386 with i387"
#define I386_HAS_FPU 1

#elif defined(i386_nofp)

#define RTEMS_MODEL_NAME  "i386 w/o i387"
#define I386_HAS_FPU 1

#elif defined(i486dx)

#define RTEMS_MODEL_NAME  "i486dx"
#define I386_HAS_FPU 1

#elif defined(i486sx)

#define RTEMS_MODEL_NAME  "i486sx"
#define I386_HAS_FPU 0

#elif defined(pentium)

#define RTEMS_MODEL_NAME  "Pentium"
#define I386_HAS_FPU 1

#else

#error "Unsupported CPU Model"

#endif

/*
 *  Define the name of the CPU family.
 */

#define CPU_NAME "Intel i386"

#ifndef ASM

/*
 *  Structure which makes it easier to deal with LxDT and SxDT instructions.
 */
 
typedef struct {
  unsigned short limit;
  unsigned short physical_address[ 2 ];
} i386_DTR_load_save_format;
 
/* See Chapter 5 - Memory Management in i386 manual */
 
typedef struct {
  unsigned short limit_0_15;
  unsigned short base_0_15;
  unsigned char  base_16_23;
  unsigned char  type_dt_dpl_p;
  unsigned char  limit_16_19_granularity;
  unsigned char  base_24_31;
}   i386_GDT_slot;
 
/* See Chapter 9 - Exceptions and Interrupts in i386 manual
 *
 *  NOTE: This is the IDT entry for interrupt gates ONLY.
 */
 
typedef struct {
  unsigned short offset_0_15;
  unsigned short segment_selector;
  unsigned char  reserved;
  unsigned char  p_dpl;
  unsigned short offset_16_31;
}   i386_IDT_slot;

/*
 *  Interrupt Level Macros
 */

#define i386_disable_interrupts( _level ) \
  { \
    _level = 0;   /* avoids warnings */ \
    asm volatile ( "pushf ; \
                    cli ; \
                    pop %0" \
                    : "=r" ((_level)) : "0" ((_level)) \
    ); \
  }

#define i386_enable_interrupts( _level )  \
  { \
    asm volatile ( "push %0 ; \
                    popf" \
                    : "=r" ((_level)) : "0" ((_level)) \
    ); \
  }

#define i386_flash_interrupts( _level ) \
  { \
    asm volatile ( "push %0 ; \
                    popf ; \
                    cli" \
                    : "=r" ((_level)) : "0" ((_level)) \
    ); \
  }

#define i386_get_interrupt_level( _level ) \
  do { \
    register unsigned32 _eflags = 0; \
    \
    asm volatile ( "pushf ; \
                    pop %0" \
                    : "=r" ((_eflags)) : "0" ((_eflags)) \
    ); \
    \
    _level = (_eflags & 0x0200) ? 0 : 1; \
  } while (0)

/*
 *  The following routine swaps the endian format of an unsigned int.
 *  It must be static so it can be referenced indirectly.
 */

static inline unsigned int i386_swap_U32(
  unsigned int value
)
{
  asm volatile( "rorw  $8,%%ax;"
                "rorl  $16,%0;"
                "rorw  $8,%%ax" : "=a" (value) : "0" (value) );
  return( value );
}

/*
 *  Segment Access Routines
 *
 *  NOTE:  Unfortunately, these are still static inlines even when the
 *         "macro" implementation of the generic code is used.
 */

static inline unsigned short i386_get_cs()
{
  register unsigned short segment = 0;

  asm volatile ( "movw %%cs,%0" : "=r" (segment) : "0" (segment) );

  return segment;
}

static inline unsigned short i386_get_ds()
{
  register unsigned short segment = 0;

  asm volatile ( "movw %%ds,%0" : "=r" (segment) : "0" (segment) );

  return segment;
}

static inline unsigned short i386_get_es()
{
  register unsigned short segment = 0;

  asm volatile ( "movw %%es,%0" : "=r" (segment) : "0" (segment) );

  return segment;
}

static inline unsigned short i386_get_ss()
{
  register unsigned short segment = 0;

  asm volatile ( "movw %%ss,%0" : "=r" (segment) : "0" (segment) );

  return segment;
}

static inline unsigned short i386_get_fs()
{
  register unsigned short segment = 0;

  asm volatile ( "movw %%fs,%0" : "=r" (segment) : "0" (segment) );

  return segment;
}

static inline unsigned short i386_get_gs()
{
  register unsigned short segment = 0;

  asm volatile ( "movw %%gs,%0" : "=r" (segment) : "0" (segment) );

  return segment;
}

/*
 *  IO Port Access Routines
 */

#define i386_outport_byte( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned char  __value = _value; \
     \
     asm volatile ( "outb %0,%1" : "=a" (__value), "=d" (__port) \
                                 : "0"   (__value), "1"  (__port) \
                  ); \
   }

#define i386_outport_word( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned short __value = _value; \
     \
     asm volatile ( "outw %0,%1" : "=a" (__value), "=d" (__port) \
                                 : "0"   (__value), "1"  (__port) \
                  ); \
   }

#define i386_outport_long( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned int  __value = _value; \
     \
     asm volatile ( "outl %0,%1" : "=a" (__value), "=d" (__port) \
                                 : "0"   (__value), "1"  (__port) \
                  ); \
   }

#define i386_inport_byte( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned char  __value = 0; \
     \
     asm volatile ( "inb %1,%0" : "=a" (__value), "=d" (__port) \
                                : "0"   (__value), "1"  (__port) \
                  ); \
     _value = __value; \
   }

#define i386_inport_word( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned short __value = 0; \
     \
     asm volatile ( "inw %1,%0" : "=a" (__value), "=d" (__port) \
                                : "0"   (__value), "1"  (__port) \
                  ); \
     _value = __value; \
   }

#define i386_inport_long( _port, _value ) \
   { register unsigned short __port  = _port; \
     register unsigned int  __value = 0; \
     \
     asm volatile ( "inl %1,%0" : "=a" (__value), "=d" (__port) \
                                : "0"   (__value), "1"  (__port) \
                  ); \
     _value = __value; \
   }

/*
 *  Descriptor Table helper routines
 */


#define i386_get_GDTR( _gdtr_address ) \
  { \
    void *_gdtr = (_gdtr_address); \
    \
    asm volatile( "sgdt   (%0)" : "=r" (_gdtr) : "0" (_gdtr) ); \
  }

#define i386_get_GDT_slot( _gdtr_base, _segment, _slot_address ) \
  { \
    register unsigned int   _gdt_slot  = (_gdtr_base) + (_segment); \
    register volatile void *_slot      = (_slot_address); \
    register unsigned int   _temporary = 0; \
    \
    asm volatile( "movl %%gs:(%0),%1 ; \
                   movl %1,(%2) ; \
                   movl %%gs:4(%0),%1 ; \
                   movl %1,4(%2)"  \
                     : "=r" (_gdt_slot), "=r" (_temporary), "=r" (_slot) \
                     : "0"  (_gdt_slot), "1"  (_temporary), "2"  (_slot) \
                );  \
  }

#define i386_set_GDT_slot( _gdtr_base, _segment, _slot_address ) \
  { \
    register unsigned int   _gdt_slot  = (_gdtr_base) + (_segment); \
    register volatile void *_slot      = (_slot_address); \
    register unsigned int   _temporary = 0; \
    \
    asm volatile( "movl (%2),%1 ; \
                   movl %1,%%gs:(%0) ; \
                   movl 4(%2),%1 ; \
                   movl %1,%%gs:4(%0) \
                  " \
                     : "=r" (_gdt_slot), "=r" (_temporary), "=r" (_slot) \
                     : "0"  (_gdt_slot), "1"  (_temporary), "2"  (_slot) \
                );  \
  }

static inline void i386_set_segment(
  unsigned short segment,
  unsigned int  base,
  unsigned int  limit
)
{
  i386_DTR_load_save_format         gdtr;
  volatile i386_GDT_slot            Gdt_slot;
  volatile i386_GDT_slot           *gdt_slot = &Gdt_slot;
  unsigned short                    tmp_segment = 0;
  unsigned int                      limit_adjusted;

  /* load physical address of the GDT */

  i386_get_GDTR( &gdtr );

  gdt_slot->type_dt_dpl_p  = 0x92;             /* present, dpl=0,      */
                                               /* application=1,       */
                                               /* type=data read/write */
  gdt_slot->limit_16_19_granularity = 0x40;    /* 32 bit segment       */

  limit_adjusted = limit;
  if ( limit > 4095 ) {
    gdt_slot->limit_16_19_granularity |= 0x80; /* set granularity bit */
    limit_adjusted /= 4096;
  }

  gdt_slot->limit_16_19_granularity |= (limit_adjusted >> 16) & 0xff;
  gdt_slot->limit_0_15               = limit_adjusted & 0xffff;

  gdt_slot->base_0_15  = base & 0xffff;
  gdt_slot->base_16_23 = (base >> 16) & 0xff;
  gdt_slot->base_24_31 = (base >> 24);

  i386_set_GDT_slot(
    gdtr.physical_address[0] + (gdtr.physical_address[1] << 16),
    segment,
    gdt_slot
  );

  /* Now, reload all segment registers so the limit takes effect. */

  asm volatile( "movw %%ds,%0 ; movw %0,%%ds
                 movw %%es,%0 ; movw %0,%%es
                 movw %%fs,%0 ; movw %0,%%fs
                 movw %%gs,%0 ; movw %0,%%gs
                 movw %%ss,%0 ; movw %0,%%ss"
                   : "=r" (tmp_segment)
                   : "0"  (tmp_segment)
              );

}

/* routines */

/*
 *  i386_Logical_to_physical
 *
 *  Converts logical address to physical address.
 */

void *i386_Logical_to_physical(
  unsigned short  segment,
  void           *address
);

/*
 *  i386_Physical_to_logical
 *
 *  Converts physical address to logical address.
 */

void *i386_Physical_to_logical(
  unsigned short  segment,
  void           *address
);

/*
 *  i386_Install_idt
 *
 *  This routine installs an IDT entry.
 */

void i386_Install_idt(
  unsigned int   source_offset,
  unsigned short destination_segment,
  unsigned int   destination_offset
);

/*
 *  "Simpler" names for a lot of the things defined in this file
 */

/* segment access routines */
 
#define get_cs()   i386_get_cs()
#define get_ds()   i386_get_ds()
#define get_es()   i386_get_es()
#define get_ss()   i386_get_ss()
#define get_fs()   i386_get_fs()
#define get_gs()   i386_get_gs()
 
#define CPU_swap_u32( _value )  i386_swap_U32( _value )
 
/* i80x86 I/O instructions */
 
#define outport_byte( _port, _value ) i386_outport_byte( _port, _value )
#define outport_word( _port, _value ) i386_outport_word( _port, _value )
#define outport_long( _port, _value ) i386_outport_long( _port, _value )
#define inport_byte( _port, _value )  i386_inport_byte( _port, _value )
#define inport_word( _port, _value )  i386_inport_word( _port, _value )
#define inport_long( _port, _value )  i386_inport_long( _port, _value )
 
/* complicated static inline functions */
 
#define get_GDTR( _gdtr_address ) \
        i386_get_GDTR( _gdtr_address )
 
#define get_GDT_slot( _gdtr_base, _segment, _slot_address ) \
        i386_get_GDT_slot( _gdtr_base, _segment, _slot_address )
 
#define set_GDT_slot( _gdtr_base, _segment, _slot_address ) \
        i386_set_GDT_slot( _gdtr_base, _segment, _slot_address )
 
#define set_segment( _segment, _base, _limit ) \
  i386_set_segment( _segment, _base, _limit )
 

#ifdef __cplusplus
}
#endif

#endif /* !ASM */

#endif
/* end of include file */