Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:41

0001 /**
0002  * @file
0003  *
0004  * @ingroup RTEMSBSPsI386
0005  *
0006  * @brief Real mode interrupt call implementation
0007  */
0008 
0009 /*
0010  * Copyright (c) 2014 - CTU in Prague
0011  *                      Jan Doležal ( dolezj21@fel.cvut.cz )
0012  *
0013  *  The license and distribution terms for this file may be
0014  *  found in the file LICENSE in this distribution or at
0015  *  http://www.rtems.org/license/LICENSE.
0016  */
0017 
0018 #include <bsp/realmode_int.h>
0019 #include <string.h>
0020 #include <rtems/score/cpu.h>
0021 
0022 /*
0023  * offsets to \a i386_realmode_interrupt_registers declared in realmode_int.h
0024  * used in inline assmbler for better readability
0025  */
0026 #define IR_EAX_OFF      "0x00"
0027 #define IR_EBX_OFF      "0x04"
0028 #define IR_ECX_OFF      "0x08"
0029 #define IR_EDX_OFF      "0x0C"
0030 #define IR_ESI_OFF      "0x10"
0031 #define IR_EDI_OFF      "0x14"
0032 #define IR_DS_OFF       "0x18"
0033 #define IR_ES_OFF       "0x1A"
0034 #define IR_FS_OFF       "0x1C"
0035 #define IR_GS_OFF       "0x1E"
0036 
0037 /*
0038  * offsets to \a rm_int_regs_bkp_param
0039  */
0040 #define BKP_ESP_OFF     "0x20"
0041 #define BKP_SS_OFF      "0x24"
0042 #define BKP_DS_OFF      "0x26"
0043 #define RM_ENTRY        "0x28"
0044 #define PM_ENTRY        "0x2C"
0045 
0046 /**
0047  * @brief parameters, results, backup values accessible in real mode
0048  *
0049  * @note Struct members not necessarily used in C. This serves also as
0050  *       layout of memory and it is used within inline assembler.
0051  */
0052 typedef struct {
0053     i386_realmode_interrupt_registers inoutregs;
0054     /** spot for back up of protected mode stack pointer */
0055     uint32_t pm_esp_bkp;
0056     /** spot for back up of protected mode stack selector */
0057     uint16_t pm_ss_bkp;
0058     /** spot for back up of protected mode data selector */
0059     uint16_t ds_bkp;
0060     /** spot for setting up long indirect jump offset
0061         to real mode from 16bit protected mode */
0062     uint16_t rm_entry;
0063     /** spot for setting up long indirect jump segment
0064         to real mode from 16bit protected mode */
0065     uint16_t rm_code_segment;
0066     /** returning offset for long indirect jump back
0067         to 32bit protected mode */
0068     uint32_t pm_entry;
0069     /** returning selector for long indirect jump back
0070         to 32bit protected mode */
0071     uint16_t pm_code_selector;
0072     /* if this struct is to be modified update offset definitions as well */
0073 } RTEMS_PACKED rm_int_regs_bkp_param;
0074 
0075 /* offsets to \a pm_bkp_and_param */
0076 #define BKP_IDTR_LIM    "0x00"
0077 #define BKP_IDTR_BASE   "0x02"
0078 #define BKP_ES_OFF      "0x06"
0079 #define BKP_FS_OFF      "0x08"
0080 #define BKP_GS_OFF      "0x0A"
0081 #define RML_ENTRY       "0x0C"
0082 #define RML_D_SEL       "0x12"
0083 #define RM_SS           "0x14"
0084 #define RM_SP           "0x16"
0085 #define RM_DS           "0x18"
0086 
0087 /**
0088  * @brief backup values, pointers/parameters accessible in protected mode
0089  *
0090  * @note Struct members not necessarily used in C. This serves also as
0091  *       layout of memory and it is used within inline assembler.
0092  *
0093  * @note GCC complains that access to packed data may not be aligned and
0094  *       fair enough. The warning is:
0095  *
0096  *   warning: taking address of packed member of 'struct <anonymous>' may
0097  *   result in an unaligned pointer value [-Waddress-of-packed-member]
0098  *
0099  * Disable the warning.
0100  */
0101 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
0102 typedef struct {
0103     /** spot for backup protected mode interrupt descriptor table register */
0104     uint16_t idtr_lim_bkp;
0105     /** @see idtr_lim_bkp */
0106     uint32_t idtr_base_bkp;
0107     /** spot to backup of ES register value in 32bit protected mode */
0108     uint16_t es_bkp;
0109     /** spot to backup of FS register value in 32bit protected mode */
0110     uint16_t fs_bkp;
0111     /** spot to backup of GS register value in 32bit protected mode */
0112     uint16_t gs_bkp;
0113     /** values for indirect jump to 16bit protected mode */
0114     uint32_t rml_entry;
0115     /** @see rml_entry */
0116     uint16_t rml_code_selector;
0117     /** data selector for 16bit protected mode */
0118     uint16_t rml_data_selector;
0119     /** values determinig location of real mode stack */
0120     uint16_t rm_stack_segment;
0121     /** @see rm_stack_segment */
0122     uint16_t rm_stack_pointer;
0123     /** data segment for real mode */
0124     uint16_t rm_data_segment;
0125 } RTEMS_PACKED pm_bkp_and_param;
0126 
0127 /* addresses where we are going to put Interrupt buffer,
0128  * parameter/returned/preserved values, stack and copy code
0129  * for calling BIOS interrupt real mode interface
0130  * The value is chosen arbitrarily in the first 640kB
0131  * to be accessible for real mode. It should be out of range
0132  * used by RTEMS because its base address is above 1MB.
0133  * It has to be above first 4kB (or better 64kB) which could
0134  * be used by BIOS.
0135  */
0136 #define REAL_MODE_SPOT   0x12000
0137 /* REAL_MODE_SPOT value is also top of real mode stack */
0138 
0139 /* buffers positions and lengths */
0140 #define DEFAULT_BUFFER_SIZE             512
0141 static void *default_rm_buffer_spot = (void *)REAL_MODE_SPOT;
0142 static uint16_t default_rm_buffer_size = DEFAULT_BUFFER_SIZE;
0143 
0144 /* real mode stack */
0145 #define STACK_SIZE                      8192
0146 #define INT_STACK_TOP                   REAL_MODE_SPOT
0147 
0148 /******************************
0149  * STACK            *         *
0150  ****************************** REAL_MODE_SPOT
0151  * INT_BUF          * 512 B   *
0152  ******************************
0153  * INT_REGs         *  50 B   *
0154  ******************************
0155  * INT_FNC          *~149 B   *
0156  ******************************/
0157 
0158 #define __DP_TYPE       uint8_t
0159 #define __DP_YES        ((__DP_TYPE)1)
0160 #define __DP_NO         ((__DP_TYPE)-1)
0161 #define __DP_FAIL       ((__DP_TYPE)0)
0162 static __DP_TYPE descsPrepared = __DP_NO;
0163 
0164 /* rml - real mode alike */
0165 #define rml_limit 0xFFFF
0166 static uint16_t rml_code_dsc_index = 0;
0167 static uint16_t rml_data_dsc_index = 0;
0168 
0169 /**
0170  * @brief Prepares real-mode like descriptors to be used for switching
0171  * to real mode.
0172  *
0173  * Descriptors will be placed to the GDT.
0174  *
0175  * @param[in] base32 32-bit physical address to be used as base for 16-bit
0176  *               protected mode descriptors
0177  * @retval __DP_YES descriptors are prepared
0178  * @retval __DP_FAIL descriptors allocation failed (GDT too small)
0179  */
0180 static __DP_TYPE prepareRMDescriptors (void *base32) {
0181     static void *prevBase = (void *)-1;
0182     /* check if descriptors were prepared already */
0183     if (descsPrepared == __DP_YES && prevBase == base32)
0184         return descsPrepared;
0185 
0186     if (descsPrepared == __DP_FAIL)
0187         return descsPrepared;
0188 
0189   /* create 'real mode like' segment descriptors, for switching to real mode */
0190     rml_code_dsc_index = i386_next_empty_gdt_entry();
0191     if (rml_code_dsc_index == 0)
0192     {
0193         /* not enough space in GDT */
0194         descsPrepared = __DP_FAIL;
0195         return descsPrepared;
0196     }
0197 
0198     segment_descriptors flags_desc;
0199     memset(&flags_desc, 0, sizeof(flags_desc));
0200     flags_desc.type                = 0xE;      /* bits 4  */
0201     flags_desc.descriptor_type     = 0x1;      /* bits 1  */
0202     flags_desc.privilege           = 0x0;      /* bits 2  */
0203     flags_desc.present             = 0x1;      /* bits 1  */
0204     flags_desc.available           = 0x0;      /* bits 1  */
0205     flags_desc.fixed_value_bits    = 0x0;      /* bits 1  */
0206     flags_desc.operation_size      = 0x0;      /* bits 1  */
0207     flags_desc.granularity         = 0x0;      /* bits 1  */
0208     i386_fill_segment_desc_base((unsigned)base32, &flags_desc);
0209     i386_fill_segment_desc_limit(rml_limit, &flags_desc);
0210     if (i386_raw_gdt_entry(rml_code_dsc_index, &flags_desc) == 0)
0211     {
0212         /* selector to GDT out of range */
0213         descsPrepared = __DP_FAIL;
0214         return descsPrepared;
0215     }
0216 
0217     rml_data_dsc_index = i386_next_empty_gdt_entry();
0218     if (rml_data_dsc_index == 0)
0219     {
0220         /* not enough space in GDT for both descriptors */
0221         descsPrepared = __DP_FAIL;
0222         return descsPrepared;
0223     }
0224 
0225     flags_desc.type                = 0x2;      /* bits 4  */
0226     if (i386_raw_gdt_entry(rml_data_dsc_index, &flags_desc) == 0)
0227     {
0228         /* selector to GDT out of range */
0229         descsPrepared = __DP_FAIL;
0230         return descsPrepared;
0231     }
0232     prevBase = base32;
0233     descsPrepared = __DP_YES;
0234     return descsPrepared;
0235 }
0236 
0237 void *i386_get_default_rm_buffer(uint16_t *size) {
0238     *size = default_rm_buffer_size;
0239     return default_rm_buffer_spot;
0240 }
0241 
0242 int i386_real_interrupt_call(uint8_t interrupt_number,
0243                              i386_realmode_interrupt_registers *ir)
0244 {
0245     uint32_t pagingon;
0246     rm_int_regs_bkp_param *int_passed_regs_spot;
0247     /* place where the code switching to realmode and executing
0248        interrupt is coppied */
0249     void *rm_swtch_code_dst;
0250     void *rm_stack_top;
0251 
0252     size_t cpLength;
0253     void *cpBeg;
0254 
0255     /* values that can be passed from protected mode are stored in this struct
0256        and they are passed later to the inline assembler executing interrupt */
0257     volatile pm_bkp_and_param pm_bkp, *pm_bkp_addr;
0258     unsigned short unused_offset;
0259 
0260     __asm__ volatile(   "\t"
0261         "movl    %%cr0, %%eax\n\t"
0262         "andl    %1, %%eax\n"
0263         : "=a"(pagingon)
0264         : "i"(CR0_PAGING)
0265     );
0266     if (pagingon)
0267         return 0;
0268 
0269     /* located under 1MB for real mode to be able to get/set values */
0270     int_passed_regs_spot = (rm_int_regs_bkp_param *)
0271                                 (default_rm_buffer_spot+default_rm_buffer_size);
0272     /* position for real mode code reallocation to the first 1MB of RAM */
0273     rm_swtch_code_dst = (void *)((uint32_t)int_passed_regs_spot +
0274                                  sizeof(*int_passed_regs_spot));
0275     rm_stack_top = (void *)INT_STACK_TOP;
0276 
0277     if (prepareRMDescriptors(int_passed_regs_spot) != __DP_YES)
0278         return 0;
0279 
0280     pm_bkp_addr = &pm_bkp;
0281     i386_Physical_to_real(
0282         rm_stack_top - STACK_SIZE,
0283         (unsigned short *)&pm_bkp.rm_stack_segment,
0284         (unsigned short *)&pm_bkp.rm_stack_pointer
0285     );
0286     pm_bkp.rm_stack_pointer += STACK_SIZE;
0287     pm_bkp.rml_code_selector = (rml_code_dsc_index<<3);
0288     pm_bkp.rml_entry = ((uint32_t)rm_swtch_code_dst -
0289                         (uint32_t)int_passed_regs_spot);
0290     pm_bkp.rml_data_selector = (rml_data_dsc_index<<3);
0291     i386_Physical_to_real(
0292         int_passed_regs_spot,
0293         (unsigned short *)&pm_bkp.rm_data_segment,
0294         &unused_offset
0295     );
0296 
0297     int_passed_regs_spot->inoutregs = *ir;
0298     /* offset from the beginning of coppied code */
0299     uint16_t rm_entry_offset;
0300     __asm__ volatile(
0301         "movw   $(rment-cp_beg), %0\n\t"
0302         : "=r"(rm_entry_offset)
0303     );
0304     i386_Physical_to_real(
0305         rm_swtch_code_dst+rm_entry_offset,
0306         (unsigned short *)&int_passed_regs_spot->rm_code_segment,
0307         (unsigned short *)&int_passed_regs_spot->rm_entry
0308     );
0309     __asm__ volatile(
0310         "movl   $(cp_end), %0\n\t"
0311         "movw   %%cs, %1\n\t"
0312         : "=mr"(int_passed_regs_spot->pm_entry),
0313           "=mr"(int_passed_regs_spot->pm_code_selector)
0314     );
0315     /* copy code for switch to real mode and
0316        executing interrupt to first MB of RAM */
0317     __asm__ volatile(   "\t"
0318         "mov    $cp_end-cp_beg, %0\n\t"
0319         "mov    $cp_beg, %1\n\t"
0320         : "=rm"(cpLength), "=rm"(cpBeg)
0321     );
0322     memcpy(rm_swtch_code_dst, cpBeg, cpLength);
0323     /* write interrupt number to be executed */
0324     uint16_t interrupt_number_off;
0325     uint8_t *interrupt_number_ptr;
0326     __asm__ volatile(   "\t"
0327         "movw   $intnum-cp_beg, %0\n\t"
0328         : "=rm"(interrupt_number_off)
0329     );
0330     interrupt_number_ptr = (uint8_t *)(rm_swtch_code_dst+interrupt_number_off);
0331     *interrupt_number_ptr = interrupt_number;
0332     /* execute code that jumps to coppied function, which switches to real mode,
0333        loads registers with values passed to interrupt and executes interrupt */
0334     __asm__ volatile(   "\t"
0335         /* backup stack */
0336         "movl    %[regs_spot], %%ebx\n\t"
0337         "movl    %%esp, "BKP_ESP_OFF"(%%ebx)\n\t"
0338         "movw    %%ss,  "BKP_SS_OFF"(%%ebx)\n\t"
0339         /* backup data selector */
0340         "movw    %%ds,  "BKP_DS_OFF"(%%ebx)\n\t"
0341         /* backup other selectors */
0342         "movl    %[pm_bkp], %%esi\n\t"
0343         "movw    %%es, "BKP_ES_OFF"(%%esi)\n\t"
0344         "movw    %%fs, "BKP_FS_OFF"(%%esi)\n\t"
0345         "movw    %%gs, "BKP_GS_OFF"(%%esi)\n\t"
0346         /* hopefully loader does not damage interrupt table on the beginning of
0347            memory; that means length: 0x3FF, base: 0x0 */
0348         /* preserve idtr */
0349         "movl    %%esi, %%eax\n\t"
0350         "addl    $"BKP_IDTR_LIM", %%eax\n\t"
0351         "cli\n\t"
0352         "sidt    (%%eax)\n\t"
0353         "movl    $rmidt, %%eax\n\t"
0354         "lidt    (%%eax)\n\t"
0355         /* prepare 'real mode like' data selector */
0356         "movw    "RML_D_SEL"(%%esi), %%ax\n\t"
0357         /* prepare real mode data segment value */
0358         "xorl    %%edx,%%edx\n\t"
0359         "movw    "RM_DS"(%%esi), %%dx\n\t"
0360         /* prepare real mode stack values */
0361         "movw    "RM_SS"(%%esi), %%cx\n\t"
0362         "movzwl  "RM_SP"(%%esi), %%esp\n\t"
0363         /* jump to copied function and */
0364         /* load 'real mode like' code selector */
0365         "ljmp   *"RML_ENTRY"(%%esi)\n"
0366 "rmidt:"/* limit and base for realmode interrupt descriptor table */
0367         ".word 0x3FF\n\t"
0368         ".long 0\n\t"
0369         /* load 'real mode like' data selectors */
0370 "cp_beg: .code16\n\t"
0371         "movw    %%ax, %%ss\n\t"
0372         "movw    %%ax, %%ds\n\t"
0373         "movw    %%ax, %%es\n\t"
0374         "movw    %%ax, %%fs\n\t"
0375         "movw    %%ax, %%gs\n\t"
0376         /* disable protected mode */
0377         "movl    %%cr0, %%eax\n\t"
0378         "and     %[cr0_prot_dis], %%ax\n\t"
0379         "movl    %%eax, %%cr0\n\t"
0380         /* base for data selector of 16-bit protected mode is
0381            at beginning of passed regs */
0382         /* flush prefetch queue by far jumping */
0383         "ljmp    *"RM_ENTRY"\n\t"
0384 "rment: "
0385         /* establish rm stack - esp was already set in 32-bit protected mode*/
0386         "movw    %%cx, %%ss\n\t"
0387         /* set data segment (value prepared in 32-bit prot mode) */
0388         "movw    %%dx, %%ds\n\t"
0389         /* count real mode pointer so we don't need to overuse address
0390            prefix (by using 32bit addresses in 16bit context) */
0391         "shll    $4,%%edx\n\t"
0392         "subl    %%edx,%%ebx\n\t"
0393         /* prepare values to be used after interrupt call */
0394         "pushw   %%bx\n\t"
0395         "pushw   %%ds\n\t"
0396         /* fill registers with parameters */
0397         "movw    " IR_DS_OFF"(%%bx), %%ax\n\t"
0398         "pushw   %%ax\n\t"
0399         "movl    "IR_EAX_OFF"(%%bx), %%eax\n\t"
0400         "movl    "IR_ECX_OFF"(%%bx), %%ecx\n\t"
0401         "movl    "IR_EDX_OFF"(%%bx), %%edx\n\t"
0402         "movl    "IR_EDI_OFF"(%%bx), %%edi\n\t"
0403         "movl    "IR_ESI_OFF"(%%bx), %%esi\n\t"
0404         "movw    " IR_ES_OFF"(%%bx), %%es\n\t"
0405         "movw    " IR_FS_OFF"(%%bx), %%fs\n\t"
0406         "movw    " IR_GS_OFF"(%%bx), %%gs\n\t"
0407         /* prepare ebx register */
0408         "movl    "IR_EBX_OFF"(%%bx), %%ebx\n\t"
0409         /* prepare ds */
0410         "popw    %%ds\n\t"
0411         /* interrupt instruction */
0412         ".byte   0xCD\n\t"
0413 "intnum: .byte   0x0\n\t"
0414         /* fill return structure */
0415         "pushw   %%ds\n\t"
0416         "pushl   %%ebx\n\t"
0417         "movw    0x6(%%esp), %%ds\n\t"
0418         "movw    0x8(%%esp),%%bx\n\t" /* regs_spot */
0419         "movl    %%eax,"IR_EAX_OFF"(%%bx)\n\t"
0420         "popl    %%eax\n\t"
0421         "movl    %%eax,"IR_EBX_OFF"(%%bx)\n\t"
0422         "movl    %%ecx,"IR_ECX_OFF"(%%bx)\n\t"
0423         "movl    %%edx,"IR_EDX_OFF"(%%bx)\n\t"
0424         "movl    %%esi,"IR_ESI_OFF"(%%bx)\n\t"
0425         "movl    %%edi,"IR_EDI_OFF"(%%bx)\n\t"
0426         "popw    %%ax\n\t"
0427         "movw    %%ax, " IR_DS_OFF"(%%bx)\n\t"
0428         "movw    %%es, " IR_ES_OFF"(%%bx)\n\t"
0429         "movw    %%fs, " IR_FS_OFF"(%%bx)\n\t"
0430         "movw    %%gs, " IR_GS_OFF"(%%bx)\n\t"
0431         /* prepare protected mode data segment */
0432         "movw    "BKP_DS_OFF"(%%bx), %%ax\n\t"
0433         /* restore protected mode stack values */
0434         "movl    "BKP_ESP_OFF"(%%bx),%%esp\n\t"
0435         "movw    "BKP_SS_OFF"(%%bx), %%dx\n\t"
0436         /* return to protected mode */
0437         "movl    %%cr0, %%ecx     \n\t"
0438         "or      %[cr0_prot_ena], %%cx\n\t"
0439         "movl    %%ecx, %%cr0     \n\t"
0440         "ljmpl   *"PM_ENTRY"(%%bx)\n\t"
0441         ".code32\n"
0442         /* reload segmentation registers */
0443 "cp_end:"
0444         "movw    %%ax, %%ds\n\t"
0445         /* restore stack segment in protected mode context */
0446         "movw    %%dx, %%ss\n\t"
0447         "movl    %[pm_bkp], %%esi\n\t"
0448         "movw    "BKP_ES_OFF"(%%esi), %%es\n\t"
0449         "movw    "BKP_FS_OFF"(%%esi), %%fs\n\t"
0450         "movw    "BKP_GS_OFF"(%%esi), %%gs\n\t"
0451         /* restore IDTR */
0452         "addl    $"BKP_IDTR_LIM", %%esi\n\t"
0453         "lidt    (%%esi)\n\t"
0454         :
0455         : [regs_spot]"m"(int_passed_regs_spot),
0456           [pm_bkp]"m"(pm_bkp_addr),
0457           [cr0_prot_ena]"i"(CR0_PROTECTION_ENABLE),
0458           [cr0_prot_dis]"i"(~CR0_PROTECTION_ENABLE)
0459         : "memory", "ebx", "ecx", "edx", "esi", "edi"
0460     );
0461     *ir = int_passed_regs_spot->inoutregs;
0462     return 1;
0463 }