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

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

i386: doxygen and comments related to VESA real mode framebuffer

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