summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libcpu/i386/cpu.c
blob: 8873de38115d16340fef697e05515a336d359f8f (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
/*
 * cpu.c  - This file contains implementation of C function to
 *          Instanciate IDT entries. More detailled information can be found
 *	    on Intel site and more precisely in the following book :
 *
 *		Pentium Processor familly
 *		Developper's Manual
 *
 *		Volume 3 : Architecture and Programming Manual
 *
 * Copyright (C) 1998  Eric Valette (valette@crf.canon.fr)
 *                     Canon Centre Recherche France.
 *
 *  The license and distribution terms for this file may be
 *  found in found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 * $Id$
 */

#include <libcpu/cpu.h>
#include <irq.h>

static rtems_raw_irq_connect_data* 	raw_irq_table;
static rtems_raw_irq_connect_data  	default_raw_irq_entry;
static interrupt_gate_descriptor   	default_idt_entry;
static rtems_raw_irq_global_settings* 	local_settings;

void create_interrupt_gate_descriptor (interrupt_gate_descriptor* idtEntry,
				       rtems_raw_irq_hdl hdl)
{
    idtEntry->low_offsets_bits	= (((unsigned) hdl) & 0xffff);
    idtEntry->segment_selector	= i386_get_cs(); 
    idtEntry->fixed_value_bits	= 0;
    idtEntry->gate_type		= 0xe; 
    idtEntry->privilege		= 0;
    idtEntry->present		= 1;
    idtEntry->high_offsets_bits	= ((((unsigned) hdl) >> 16) & 0xffff);
}

rtems_raw_irq_hdl get_hdl_from_vector(rtems_vector_offset index)
{
    rtems_raw_irq_hdl		hdl;
    interrupt_gate_descriptor* 	idt_entry_tbl;
    unsigned			limit;

    i386_get_info_from_IDTR (&idt_entry_tbl, &limit);
  
    * ((unsigned int*) &hdl) = (idt_entry_tbl[index].low_offsets_bits | 
			      (idt_entry_tbl[index].high_offsets_bits << 16));
    return hdl;
}

int i386_set_idt_entry  (const rtems_raw_irq_connect_data* irq)
{
    interrupt_gate_descriptor* 	idt_entry_tbl;
    unsigned			limit;
    unsigned int 		level;

    
    i386_get_info_from_IDTR (&idt_entry_tbl, &limit);

    if (irq->idtIndex > limit) {
      return 0;
    }
    /*
     * Check if default handler is actually connected. If not issue an error.
     * You must first get the current handler via i386_get_current_idt_entry
     * and then disconnect it using i386_delete_idt_entry.
     * RATIONALE : to always have the same transition by forcing the user
     * to get the previous handler before accepting to disconnect.
     */
    if (get_hdl_from_vector(irq->idtIndex) != default_raw_irq_entry.hdl) {
      return 0;
    }
    _CPU_ISR_Disable(level);
    
    raw_irq_table [irq->idtIndex] = *irq;
    create_interrupt_gate_descriptor (&idt_entry_tbl[irq->idtIndex], irq->hdl);
    irq->on(irq);
    
    _CPU_ISR_Enable(level);
    return 1;
}

void _CPU_ISR_install_vector (unsigned vector,
			      void* hdl,
			      void** oldHdl)
{
    interrupt_gate_descriptor* 	idt_entry_tbl;
    unsigned			limit;
    interrupt_gate_descriptor	new;
    unsigned int 		level;
    
    i386_get_info_from_IDTR (&idt_entry_tbl, &limit);

    if (vector > limit) {
      return;
    }
    _CPU_ISR_Disable(level)
    * ((unsigned int *) oldHdl) = idt_entry_tbl[vector].low_offsets_bits |
	(idt_entry_tbl[vector].high_offsets_bits << 16);

    create_interrupt_gate_descriptor(&new,  hdl);
    idt_entry_tbl[vector] = new;

    _CPU_ISR_Enable(level);
}
				
int i386_get_current_idt_entry (rtems_raw_irq_connect_data* irq)
{
    interrupt_gate_descriptor* 	idt_entry_tbl;
    unsigned			limit;

    i386_get_info_from_IDTR (&idt_entry_tbl, &limit);

    if (irq->idtIndex > limit) {
      return 1;
    }
    raw_irq_table [irq->idtIndex].hdl = get_hdl_from_vector(irq->idtIndex);
    
    *irq = raw_irq_table [irq->idtIndex];
    
    return 0;
}

int i386_delete_idt_entry (const rtems_raw_irq_connect_data* irq)
{
    interrupt_gate_descriptor* 	idt_entry_tbl;
    unsigned			limit;
    unsigned int 		level;
    
    i386_get_info_from_IDTR (&idt_entry_tbl, &limit);

    if (irq->idtIndex > limit) {
      return 1;
    }
     /*
     * Check if handler passed is actually connected. If not issue an error.
     * You must first get the current handler via i386_get_current_idt_entry
     * and then disconnect it using i386_delete_idt_entry.
     * RATIONALE : to always have the same transition by forcing the user
     * to get the previous handler before accepting to disconnect.
     */
    if (get_hdl_from_vector(irq->idtIndex) != irq->hdl){
      return 1;
    }
    _CPU_ISR_Disable(level);

    idt_entry_tbl[irq->idtIndex] = default_idt_entry;

    irq->off(irq);
    
    raw_irq_table[irq->idtIndex] = default_raw_irq_entry;

    _CPU_ISR_Enable(level);
    
    return 0;
}

/*
 * Caution this function assumes the IDTR has been already set.
 */
int i386_init_idt (rtems_raw_irq_global_settings* config)
{
    unsigned			limit;
    unsigned 			i;
    unsigned 			level;
    interrupt_gate_descriptor*	idt_entry_tbl;
    
    i386_get_info_from_IDTR (&idt_entry_tbl, &limit);

      
    if (config->idtSize != limit) {
      return 1;
    }
    /*
     * store various accelarators
     */
    raw_irq_table 		= config->rawIrqHdlTbl;
    local_settings 		= config;
    default_raw_irq_entry 	= config->defaultRawEntry;

    _CPU_ISR_Disable(level);

    create_interrupt_gate_descriptor (&default_idt_entry, default_raw_irq_entry.hdl);

    for (i=0; i < limit; i++) {
      interrupt_gate_descriptor new;
      create_interrupt_gate_descriptor (&new, raw_irq_table[i].hdl);
      idt_entry_tbl[i] = new;
      if (raw_irq_table[i].hdl != default_raw_irq_entry.hdl) {
	raw_irq_table[i].on(&raw_irq_table[i]);
      }
      else {
	raw_irq_table[i].off(&raw_irq_table[i]);
      }
    }
    _CPU_ISR_Enable(level);

    return 0;
}

int i386_get_idt_config (rtems_raw_irq_global_settings** config)
{
  *config = local_settings;
  return 0;
}

/*
 * Caution this function assumes the GDTR has been already set.
 */
int i386_set_gdt_entry (unsigned short segment_selector, unsigned base,
			unsigned limit)
{
    unsigned 			gdt_limit;
    unsigned short              tmp_segment = 0;
    unsigned int                limit_adjusted;
    segment_descriptors* 	gdt_entry_tbl;

      
    i386_get_info_from_GDTR (&gdt_entry_tbl, &gdt_limit);

    if (segment_selector > limit) {
      return 1;
    }
    /*
     * set up limit first
     */
    limit_adjusted = limit;
    if ( limit > 4095 ) {
      gdt_entry_tbl[segment_selector].granularity = 1;
      limit_adjusted /= 4096;
    }
    gdt_entry_tbl[segment_selector].limit_15_0  = limit_adjusted & 0xffff;
    gdt_entry_tbl[segment_selector].limit_19_16 = (limit_adjusted >> 16) & 0xf;
    /*
     * set up base
     */
    gdt_entry_tbl[segment_selector].base_address_15_0  = base & 0xffff;
    gdt_entry_tbl[segment_selector].base_address_23_16 = (base >> 16) & 0xff;
    gdt_entry_tbl[segment_selector].base_address_31_24 = (base >> 24) & 0xff;
    /*
     * set up descriptor type (this may well becomes a parameter if needed)
     */
    gdt_entry_tbl[segment_selector].type 		= 2;   	/* Data R/W */
    gdt_entry_tbl[segment_selector].descriptor_type 	= 1;	/* Code or Data */
    gdt_entry_tbl[segment_selector].privilege 		= 0; 	/* ring 0 */
    gdt_entry_tbl[segment_selector].present 		= 1; 	/* not present */

    /*
     * 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)
		  );
    
    return 0;
}