source: rtems/c/src/lib/libbsp/i386/shared/realmode_int/realmode_int.c @ d78eac6

4.115
Last change on this file since d78eac6 was d78eac6, checked in by Jan Dolezal <dolezj21@…>, on Dec 3, 2014 at 11:56:38 PM

i386/pc386: cammelCase (struct and function names) to underscores, typedefed structs, break >80 chars lines, removed newlines at EOFs

fb_vesa_rm.c: removed inline from functions declared in fb_vesa.h

removed unnecessary printks in the end of patch

edid.h, vbe3.h: switched from custom *PACKED_ATTRIBUTE at the structs to the

RTEMS_COMPILER_PACKED_ATTRIBUTE for easier maintainability
of doxygen

  • Property mode set to 100644
File size: 14.2 KB
Line 
1/*
2 *  Realmode interrupt call implementation.
3 *
4 *
5 *  Copyright (c) 2014 - CTU in Prague
6 *                       Jan DoleÅŸal ( dolezj21@fel.cvut.cz )
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.org/license/LICENSE.
11 *
12 */
13
14#include <bsp/realmode_int.h>
15#include <string.h>
16#include <rtems/score/cpu.h>
17
18#define IR_EAX_OFF      "0x00"
19#define IR_EBX_OFF      "0x04"
20#define IR_ECX_OFF      "0x08"
21#define IR_EDX_OFF      "0x0C"
22#define IR_ESI_OFF      "0x10"
23#define IR_EDI_OFF      "0x14"
24#define IR_DS_OFF       "0x18"
25#define IR_ES_OFF       "0x1A"
26#define IR_FS_OFF       "0x1C"
27#define IR_GS_OFF       "0x1E"
28
29#define BKP_ESP_OFF     "0x20"
30#define BKP_SS_OFF      "0x24"
31#define BKP_DS_OFF      "0x26"
32#define RM_ENTRY        "0x28"
33#define PM_ENTRY        "0x2C"
34
35/* parameters, results, backup values accessible in real mode */
36typedef struct {
37    i386_realmode_interrupt_registers inoutregs;
38    uint32_t pm_esp_bkp;
39    uint16_t pm_ss_bkp;
40    uint16_t ds_bkp;
41    uint16_t rm_entry;
42    uint16_t rm_code_segment;
43    uint32_t pm_entry;
44    uint16_t pm_code_selector;
45    /* if modifying update offset definitions as well */
46} RTEMS_COMPILER_PACKED_ATTRIBUTE rm_int_regs_bkp_param;
47
48#define BKP_IDTR_LIM    "0x00"
49#define BKP_IDTR_BASE   "0x02"
50#define BKP_ES_OFF      "0x06"
51#define BKP_FS_OFF      "0x08"
52#define BKP_GS_OFF      "0x0A"
53#define RML_ENTRY       "0x0C"
54#define RML_D_SEL       "0x12"
55#define RM_SS           "0x14"
56#define RM_SP           "0x16"
57#define RM_DS           "0x18"
58/* backup values, pointers/parameters accessible in protected mode */
59typedef struct {
60    uint16_t idtr_lim_bkp;
61    uint32_t idtr_base_bkp;
62    uint16_t es_bkp;
63    uint16_t fs_bkp;
64    uint16_t gs_bkp;
65    uint32_t rml_entry;
66    uint16_t rml_code_selector;
67    uint16_t rml_data_selector;
68    uint16_t rm_stack_segment;
69    uint16_t rm_stack_pointer;
70    uint16_t rm_data_segment;
71} RTEMS_COMPILER_PACKED_ATTRIBUTE pm_bkp_and_param;
72
73/* addresses where we are going to put Interrupt buffer,
74 * parameter/returned/preserved values, stack and copy code
75 * for calling BIOS interrupt real mode interface
76 * The value is chosen arbitrarily in the first 640kB
77 * to be accessible for real mode. It should be out of range
78 * used by RTEMS because its base address is above 1MB.
79 * It has to be above first 4kB (or better 64kB) which could
80 * be used by BIOS.
81 */
82#define REAL_MODE_SPOT   0x12000
83/* REAL_MODE_SPOT value is also top of real mode stack */
84
85/* buffers positions and lengths */
86#define DEFAULT_BUFFER_SIZE             512
87static void *default_rm_buffer_spot = (void *)REAL_MODE_SPOT;
88static uint16_t default_rm_buffer_size = DEFAULT_BUFFER_SIZE;
89
90/* real mode stack */
91#define STACK_SIZE                      8192
92#define INT_STACK_TOP                   REAL_MODE_SPOT
93
94/******************************
95 * STACK            *         *
96 ****************************** REAL_MODE_SPOT
97 * INT_BUF          * 512 B   *
98 ******************************
99 * INT_REGs         *  50 B   *
100 ******************************
101 * INT_FNC          *~149 B   *
102 ******************************/
103
104#define __DP_TYPE       uint8_t
105#define __DP_YES        ((__DP_TYPE)1)
106#define __DP_NO         ((__DP_TYPE)-1)
107#define __DP_FAIL       ((__DP_TYPE)0)
108static __DP_TYPE descsPrepared = __DP_NO;
109
110/* rml - real mode alike */
111#define rml_limit 0xFFFF
112static uint16_t rml_code_dsc_index = 0;
113static uint16_t rml_data_dsc_index = 0;
114
115/*
116 * Prepares real-mode like descriptors to be used for switching
117 * to real mode.
118 *
119 * @retval __DP_YES descriptors are prepared
120 * @retval __DP_FAIL descriptors allocation failed (GDT too small)
121 */
122static __DP_TYPE prepareRMDescriptors (void *base32) {
123    static void *prevBase = (void *)-1;
124    /* check if descriptors were prepared already */
125    if (descsPrepared == __DP_YES && prevBase == base32)
126        return descsPrepared;
127
128    if (descsPrepared == __DP_FAIL)
129        return descsPrepared;
130
131  /* create 'real mode like' segment descriptors, for switching to real mode */
132    rml_code_dsc_index = i386_next_empty_gdt_entry();
133    if (rml_code_dsc_index == 0)
134    {
135        /* not enough space in GDT */
136        descsPrepared = __DP_FAIL;
137        return descsPrepared;
138    }
139
140    segment_descriptors flags_desc;
141    memset(&flags_desc, 0, sizeof(flags_desc));
142    flags_desc.type                = 0xE;      /* bits 4  */
143    flags_desc.descriptor_type     = 0x1;      /* bits 1  */
144    flags_desc.privilege           = 0x0;      /* bits 2  */
145    flags_desc.present             = 0x1;      /* bits 1  */
146    flags_desc.available           = 0x0;      /* bits 1  */
147    flags_desc.fixed_value_bits    = 0x0;      /* bits 1  */
148    flags_desc.operation_size      = 0x0;      /* bits 1  */
149    flags_desc.granularity         = 0x0;      /* bits 1  */
150    i386_fill_segment_desc_base((unsigned)base32, &flags_desc);
151    i386_fill_segment_desc_limit(rml_limit, &flags_desc);
152    if (i386_raw_gdt_entry(rml_code_dsc_index, &flags_desc) == 0)
153    {
154        /* selector to GDT out of range */
155        descsPrepared = __DP_FAIL;
156        return descsPrepared;
157    }
158
159    rml_data_dsc_index = i386_next_empty_gdt_entry();
160    if (rml_data_dsc_index == 0)
161    {
162        /* not enough space in GDT for both descriptors */
163        descsPrepared = __DP_FAIL;
164        return descsPrepared;
165    }
166
167    flags_desc.type                = 0x2;      /* bits 4  */
168    if (i386_raw_gdt_entry(rml_data_dsc_index, &flags_desc) == 0)
169    {
170        /* selector to GDT out of range */
171        descsPrepared = __DP_FAIL;
172        return descsPrepared;
173    }
174    prevBase = base32;
175    descsPrepared = __DP_YES;
176    return descsPrepared;
177}
178
179void *i386_get_default_rm_buffer(uint16_t *size) {
180    *size = default_rm_buffer_size;
181    return default_rm_buffer_spot;
182}
183
184int i386_real_interrupt_call(uint8_t interrupt_number,
185                             i386_realmode_interrupt_registers *ir)
186{
187    uint32_t pagingon;
188    rm_int_regs_bkp_param *int_passed_regs_spot;
189    /* place where the code switching to realmode and executing
190       interrupt is coppied */
191    void *rm_swtch_code_dst;
192    void *rm_stack_top;
193
194    size_t cpLength;
195    void *cpBeg;
196
197    /* values that can be passed from protected mode are stored in this struct
198       and they are passed later to the inline assembler executing interrupt */
199    volatile pm_bkp_and_param pm_bkp, *pm_bkp_addr;
200    unsigned short unused_offset;
201
202    __asm__ volatile(   "\t"
203        "movl    %%cr0, %%eax\n\t"
204        "andl    %1, %%eax\n"
205        : "=a"(pagingon)
206        : "i"(CR0_PAGING)
207    );
208    if (pagingon)
209        return 0;
210
211    /* located under 1MB for real mode to be able to get/set values */
212    int_passed_regs_spot = (rm_int_regs_bkp_param *)
213                                (default_rm_buffer_spot+default_rm_buffer_size);
214    /* position for real mode code reallocation to the first 1MB of RAM */
215    rm_swtch_code_dst = (void *)((uint32_t)int_passed_regs_spot +
216                                 sizeof(*int_passed_regs_spot));
217    rm_stack_top = (void *)INT_STACK_TOP;
218
219    if (prepareRMDescriptors(int_passed_regs_spot) != __DP_YES)
220        return 0;
221
222    pm_bkp_addr = &pm_bkp;
223    i386_Physical_to_real(
224        rm_stack_top - STACK_SIZE,
225        (unsigned short *)&pm_bkp.rm_stack_segment,
226        (unsigned short *)&pm_bkp.rm_stack_pointer
227    );
228    pm_bkp.rm_stack_pointer += STACK_SIZE;
229    pm_bkp.rml_code_selector = (rml_code_dsc_index<<3);
230    pm_bkp.rml_entry = ((uint32_t)rm_swtch_code_dst -
231                        (uint32_t)int_passed_regs_spot);
232    pm_bkp.rml_data_selector = (rml_data_dsc_index<<3);
233    i386_Physical_to_real(
234        int_passed_regs_spot,
235        (unsigned short *)&pm_bkp.rm_data_segment,
236        &unused_offset
237    );
238
239    int_passed_regs_spot->inoutregs = *ir;
240    /* offset from the beginning of coppied code */
241    uint16_t rm_entry_offset;
242    __asm__ volatile(
243        "movw   $(rment-cp_beg), %0\n\t"
244        : "=r"(rm_entry_offset)
245    );
246    i386_Physical_to_real(
247        rm_swtch_code_dst+rm_entry_offset,
248        (unsigned short *)&int_passed_regs_spot->rm_code_segment,
249        (unsigned short *)&int_passed_regs_spot->rm_entry
250    );
251    __asm__ volatile(
252        "movl   $(cp_end), %0\n\t"
253        "movw   %%cs, %1\n\t"
254        : "=mr"(int_passed_regs_spot->pm_entry),
255          "=mr"(int_passed_regs_spot->pm_code_selector)
256    );
257    /* copy code for switch to real mode and
258       executing interrupt to first MB of RAM */
259    __asm__ volatile(   "\t"
260        "mov    $cp_end-cp_beg, %0\n\t"
261        "mov    $cp_beg, %1\n\t"
262        : "=rm"(cpLength), "=rm"(cpBeg)
263    );
264    memcpy(rm_swtch_code_dst, cpBeg, cpLength);
265    /* write interrupt number to be executed */
266    uint16_t interrupt_number_off;
267    uint8_t *interrupt_number_ptr;
268    __asm__ volatile(   "\t"
269        "movw   $intnum-cp_beg, %0\n\t"
270        : "=rm"(interrupt_number_off)
271    );
272    interrupt_number_ptr = (uint8_t *)(rm_swtch_code_dst+interrupt_number_off);
273    *interrupt_number_ptr = interrupt_number;
274    /* execute code that jumps to coppied function, which switches to real mode,
275       loads registers with values passed to interrupt and executes interrupt */
276    __asm__ volatile(   "\t"
277        /* backup stack */
278        "movl    %[regs_spot], %%ebx\n\t"
279        "movl    %%esp, "BKP_ESP_OFF"(%%ebx)\n\t"
280        "movw    %%ss,  "BKP_SS_OFF"(%%ebx)\n\t"
281        /* backup data selector */
282        "movw    %%ds,  "BKP_DS_OFF"(%%ebx)\n\t"
283        /* backup other selectors */
284        "movl    %[pm_bkp], %%esi\n\t"
285        "movw    %%es, "BKP_ES_OFF"(%%esi)\n\t"
286        "movw    %%fs, "BKP_FS_OFF"(%%esi)\n\t"
287        "movw    %%gs, "BKP_GS_OFF"(%%esi)\n\t"
288        /* hopefully loader does not damage interrupt table on the beginning of
289           memory; that means length: 0x3FF, base: 0x0 */
290        /* preserve idtr */
291        "movl    %%esi, %%eax\n\t"
292        "addl    $"BKP_IDTR_LIM", %%eax\n\t"
293        "cli\n\t"
294        "sidt    (%%eax)\n\t"
295        "movl    $rmidt, %%eax\n\t"
296        "lidt    (%%eax)\n\t"
297        /* prepare 'real mode like' data selector */
298        "movw    "RML_D_SEL"(%%esi), %%ax\n\t"
299        /* prepare real mode data segment value */
300        "xorl    %%edx,%%edx\n\t"
301        "movw    "RM_DS"(%%esi), %%dx\n\t"
302        /* prepare real mode stack values */
303        "movw    "RM_SS"(%%esi), %%cx\n\t"
304        "movzwl  "RM_SP"(%%esi), %%esp\n\t"
305        /* jump to copied function and */
306        /* load 'real mode like' code selector */
307        "ljmp   *"RML_ENTRY"(%%esi)\n"
308"rmidt:"/* limit and base for realmode interrupt descriptor table */
309        ".word 0x3FF\n\t"
310        ".long 0\n\t"
311        /* load 'real mode like' data selectors */
312"cp_beg: .code16\n\t"
313        "movw    %%ax, %%ss\n\t"
314        "movw    %%ax, %%ds\n\t"
315        "movw    %%ax, %%es\n\t"
316        "movw    %%ax, %%fs\n\t"
317        "movw    %%ax, %%gs\n\t"
318        /* disable protected mode */
319        "movl    %%cr0, %%eax\n\t"
320        "and     %[cr0_prot_dis], %%ax\n\t"
321        "movl    %%eax, %%cr0\n\t"
322        /* base for data selector of 16-bit protected mode is
323           at beginning of passed regs */
324        /* flush prefetch queue by far jumping */
325        "ljmp    *"RM_ENTRY"\n\t"
326"rment: "
327        /* establish rm stack - esp was already set in 32-bit protected mode*/
328        "movw    %%cx, %%ss\n\t"
329        /* set data segment (value prepared in 32-bit prot mode) */
330        "movw    %%dx, %%ds\n\t"
331        /* count real mode pointer so we don't need to overuse address
332           prefix (by using 32bit addresses in 16bit context) */
333        "shll    $4,%%edx\n\t"
334        "subl    %%edx,%%ebx\n\t"
335        /* prepare values to be used after interrupt call */
336        "pushw   %%bx\n\t"
337        "pushw   %%ds\n\t"
338        /* fill registers with parameters */
339        "movw    " IR_DS_OFF"(%%bx), %%ax\n\t"
340        "pushw   %%ax\n\t"
341        "movl    "IR_EAX_OFF"(%%bx), %%eax\n\t"
342        "movl    "IR_ECX_OFF"(%%bx), %%ecx\n\t"
343        "movl    "IR_EDX_OFF"(%%bx), %%edx\n\t"
344        "movl    "IR_EDI_OFF"(%%bx), %%edi\n\t"
345        "movl    "IR_ESI_OFF"(%%bx), %%esi\n\t"
346        "movw    " IR_ES_OFF"(%%bx), %%es\n\t"
347        "movw    " IR_FS_OFF"(%%bx), %%fs\n\t"
348        "movw    " IR_GS_OFF"(%%bx), %%gs\n\t"
349        /* prepare ebx register */
350        "movl    "IR_EBX_OFF"(%%bx), %%ebx\n\t"
351        /* prepare ds */
352        "popw    %%ds\n\t"
353        /* interrupt instruction */
354        ".byte   0xCD\n\t"
355"intnum: .byte   0x0\n\t"
356        /* fill return structure */
357        "pushw   %%ds\n\t"
358        "pushl   %%ebx\n\t"
359        "movw    0x6(%%esp), %%ds\n\t"
360        "movw    0x8(%%esp),%%bx\n\t" /* regs_spot */
361        "movl    %%eax,"IR_EAX_OFF"(%%bx)\n\t"
362        "popl    %%eax\n\t"
363        "movl    %%eax,"IR_EBX_OFF"(%%bx)\n\t"
364        "movl    %%ecx,"IR_ECX_OFF"(%%bx)\n\t"
365        "movl    %%edx,"IR_EDX_OFF"(%%bx)\n\t"
366        "movl    %%esi,"IR_ESI_OFF"(%%bx)\n\t"
367        "movl    %%edi,"IR_EDI_OFF"(%%bx)\n\t"
368        "popw    %%ax\n\t"
369        "movw    %%ax, " IR_DS_OFF"(%%bx)\n\t"
370        "movw    %%es, " IR_ES_OFF"(%%bx)\n\t"
371        "movw    %%fs, " IR_FS_OFF"(%%bx)\n\t"
372        "movw    %%gs, " IR_GS_OFF"(%%bx)\n\t"
373        /* prepare protected mode data segment */
374        "movw    "BKP_DS_OFF"(%%bx), %%ax\n\t"
375        /* restore protected mode stack values */
376        "movl    "BKP_ESP_OFF"(%%bx),%%esp\n\t"
377        "movw    "BKP_SS_OFF"(%%bx), %%dx\n\t"
378        /* return to protected mode */
379        "movl    %%cr0, %%ecx     \n\t"
380        "or      %[cr0_prot_ena], %%cx\n\t"
381        "movl    %%ecx, %%cr0     \n\t"
382        "ljmpl   *"PM_ENTRY"(%%bx)\n\t"
383        ".code32\n"
384        /* reload segmentation registers */
385"cp_end:"
386        "movw    %%ax, %%ds\n\t"
387        /* restore stack segment in protected mode context */
388        "movw    %%dx, %%ss\n\t"
389        "movl    %[pm_bkp], %%esi\n\t"
390        "movw    "BKP_ES_OFF"(%%esi), %%es\n\t"
391        "movw    "BKP_FS_OFF"(%%esi), %%fs\n\t"
392        "movw    "BKP_GS_OFF"(%%esi), %%gs\n\t"
393        /* restore IDTR */
394        "addl    $"BKP_IDTR_LIM", %%esi\n\t"
395        "lidt    (%%esi)\n\t"
396        :
397        : [regs_spot]"m"(int_passed_regs_spot),
398          [pm_bkp]"m"(pm_bkp_addr),
399          [cr0_prot_ena]"i"(CR0_PROTECTION_ENABLE),
400          [cr0_prot_dis]"i"(~CR0_PROTECTION_ENABLE)
401        : "memory", "ebx", "ecx", "edx", "esi", "edi"
402    );
403    *ir = int_passed_regs_spot->inoutregs;
404    return 1;
405}
Note: See TracBrowser for help on using the repository browser.