File indexing completed on 2025-05-11 08:23:41
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <bsp/realmode_int.h>
0019 #include <string.h>
0020 #include <rtems/score/cpu.h>
0021
0022
0023
0024
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
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
0048
0049
0050
0051
0052 typedef struct {
0053 i386_realmode_interrupt_registers inoutregs;
0054
0055 uint32_t pm_esp_bkp;
0056
0057 uint16_t pm_ss_bkp;
0058
0059 uint16_t ds_bkp;
0060
0061
0062 uint16_t rm_entry;
0063
0064
0065 uint16_t rm_code_segment;
0066
0067
0068 uint32_t pm_entry;
0069
0070
0071 uint16_t pm_code_selector;
0072
0073 } RTEMS_PACKED rm_int_regs_bkp_param;
0074
0075
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
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
0102 typedef struct {
0103
0104 uint16_t idtr_lim_bkp;
0105
0106 uint32_t idtr_base_bkp;
0107
0108 uint16_t es_bkp;
0109
0110 uint16_t fs_bkp;
0111
0112 uint16_t gs_bkp;
0113
0114 uint32_t rml_entry;
0115
0116 uint16_t rml_code_selector;
0117
0118 uint16_t rml_data_selector;
0119
0120 uint16_t rm_stack_segment;
0121
0122 uint16_t rm_stack_pointer;
0123
0124 uint16_t rm_data_segment;
0125 } RTEMS_PACKED pm_bkp_and_param;
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136 #define REAL_MODE_SPOT 0x12000
0137
0138
0139
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
0145 #define STACK_SIZE 8192
0146 #define INT_STACK_TOP REAL_MODE_SPOT
0147
0148
0149
0150
0151
0152
0153
0154
0155
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
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
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 static __DP_TYPE prepareRMDescriptors (void *base32) {
0181 static void *prevBase = (void *)-1;
0182
0183 if (descsPrepared == __DP_YES && prevBase == base32)
0184 return descsPrepared;
0185
0186 if (descsPrepared == __DP_FAIL)
0187 return descsPrepared;
0188
0189
0190 rml_code_dsc_index = i386_next_empty_gdt_entry();
0191 if (rml_code_dsc_index == 0)
0192 {
0193
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;
0201 flags_desc.descriptor_type = 0x1;
0202 flags_desc.privilege = 0x0;
0203 flags_desc.present = 0x1;
0204 flags_desc.available = 0x0;
0205 flags_desc.fixed_value_bits = 0x0;
0206 flags_desc.operation_size = 0x0;
0207 flags_desc.granularity = 0x0;
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
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
0221 descsPrepared = __DP_FAIL;
0222 return descsPrepared;
0223 }
0224
0225 flags_desc.type = 0x2;
0226 if (i386_raw_gdt_entry(rml_data_dsc_index, &flags_desc) == 0)
0227 {
0228
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
0248
0249 void *rm_swtch_code_dst;
0250 void *rm_stack_top;
0251
0252 size_t cpLength;
0253 void *cpBeg;
0254
0255
0256
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
0270 int_passed_regs_spot = (rm_int_regs_bkp_param *)
0271 (default_rm_buffer_spot+default_rm_buffer_size);
0272
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
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
0316
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
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
0333
0334 __asm__ volatile( "\t"
0335
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
0340 "movw %%ds, "BKP_DS_OFF"(%%ebx)\n\t"
0341
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
0347
0348
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
0356 "movw "RML_D_SEL"(%%esi), %%ax\n\t"
0357
0358 "xorl %%edx,%%edx\n\t"
0359 "movw "RM_DS"(%%esi), %%dx\n\t"
0360
0361 "movw "RM_SS"(%%esi), %%cx\n\t"
0362 "movzwl "RM_SP"(%%esi), %%esp\n\t"
0363
0364
0365 "ljmp *"RML_ENTRY"(%%esi)\n"
0366 "rmidt:"
0367 ".word 0x3FF\n\t"
0368 ".long 0\n\t"
0369
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
0377 "movl %%cr0, %%eax\n\t"
0378 "and %[cr0_prot_dis], %%ax\n\t"
0379 "movl %%eax, %%cr0\n\t"
0380
0381
0382
0383 "ljmp *"RM_ENTRY"\n\t"
0384 "rment: "
0385
0386 "movw %%cx, %%ss\n\t"
0387
0388 "movw %%dx, %%ds\n\t"
0389
0390
0391 "shll $4,%%edx\n\t"
0392 "subl %%edx,%%ebx\n\t"
0393
0394 "pushw %%bx\n\t"
0395 "pushw %%ds\n\t"
0396
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
0408 "movl "IR_EBX_OFF"(%%bx), %%ebx\n\t"
0409
0410 "popw %%ds\n\t"
0411
0412 ".byte 0xCD\n\t"
0413 "intnum: .byte 0x0\n\t"
0414
0415 "pushw %%ds\n\t"
0416 "pushl %%ebx\n\t"
0417 "movw 0x6(%%esp), %%ds\n\t"
0418 "movw 0x8(%%esp),%%bx\n\t"
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
0432 "movw "BKP_DS_OFF"(%%bx), %%ax\n\t"
0433
0434 "movl "BKP_ESP_OFF"(%%bx),%%esp\n\t"
0435 "movw "BKP_SS_OFF"(%%bx), %%dx\n\t"
0436
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
0443 "cp_end:"
0444 "movw %%ax, %%ds\n\t"
0445
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
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 }