OSDev.org
https://forum.osdev.org/

create IDT in Assembly
https://forum.osdev.org/viewtopic.php?f=13&t=39634
Page 1 of 1

Author:  growlnx [ Sun Jan 10, 2021 10:12 pm ]
Post subject:  create IDT in Assembly

Hello everybody!

I know in C is more easy but i'm trying to implement the IDT in pure assembly for learning purposes.

Code:
%ifndef IDT
   %define IDT

extern K_int_handler

idt_start:
   %assign i 0
   %rep 32
      irq %+i:
         dw  <ADDRESS OF ISR> & 0FFFFh
         dw DATA_SEG ; 0x08
         db 0
         db 08eh
         dw <ADDRESS OF ISR> & 0FFFF0000h) >> 16
      %assign i i+1
   %endrep

   times (255-31) resb 0 ; fill rest of idt

idt_end:

idtr:
   dw idt_end - idt_start - 1
   dd idt_start

%assign i 0
%rep 32
   isr %+i:
      cli
      pusha
      call K_int_handler
      popa
      sti
      iret
   %assign i i+1
%endrep

idt_setup:
   pusha
   cli
   lidt [idtr]
   sti
   popa
   ret

%endif ; IDT


I want put the address of isr* in "<ADDRESS OF ISR>", does anyone have any idea how to do?


Author:  Octocontrabass [ Sun Jan 10, 2021 10:33 pm ]
Post subject:  Re: create IDT in Assembly

growlnx wrote:
I want put the address of isr* in "<ADDRESS OF ISR>", does anyone have any idea how to do?

If you're assembling a flat binary, you can perform arithmetic on labels.

If you're assembling a proper object file to eventually link with other object files into a complete kernel binary, it's not possible. There are no x86 object file formats that support relocations for half of an address.

Author:  growlnx [ Sun Jan 10, 2021 10:48 pm ]
Post subject:  Re: create IDT in Assembly

it will be a elf_i386 binary kernel to be loaded with GRUB.

Author:  growlnx [ Sun Jan 10, 2021 10:53 pm ]
Post subject:  Re: create IDT in Assembly

Any ideas on how to get around this problem without resorting to high level?

Author:  bzt [ Mon Jan 11, 2021 7:33 am ]
Post subject:  Re: create IDT in Assembly

growlnx wrote:
Any ideas on how to get around this problem without resorting to high level?
You can calculate the IDT entries in run-time instead of using macros. But I'd like to point out that it is very uncommon not to load the kernel at fixed address, therefore chances are good no relocation needed no matter the executable format (elf, pe, coff, aout etc.). In i386 elf (if it's not compiled as shared library) then the program headers tells the loader (GRUB) where to load the segments.

Anyway, if you really want a relocatable kernel, then something like:
Code:
mov edi, idt_start

mov eax, label1           ; relocateable, as it has the entire label
call store_idt

mov eax, label2
call store_idt
; ... etc.

; --- store one IDT entry IN: edi=ptr to idt, eax=label OUT: edi=incremented ---
store_idt:
  mov ebx, eax            ; do the shifting and masking in run-time
  stosw                   ; store first part in IDT
  mov eax, 08e0008
  stosd

  mov eax, ebx
  shr eax, 16
  stosw                   ; store second part in IDT
  ret
Note these are not necessarily valid IDT operations, just examples. Do not copy'n'paste store_idt as-is.

Cheers,
bzt

Author:  Octocontrabass [ Mon Jan 11, 2021 10:48 am ]
Post subject:  Re: create IDT in Assembly

growlnx wrote:
Any ideas on how to get around this problem without resorting to high level?

Everything you can do in a high-level language you can do in assembly.

But if you're sure you'll never want to generate IDT entries at runtime, you can always rearrange the IDT entries. For example, exchange the ISR segment and the upper 16 bits of the ISR offset in your code (that way the ISR offset isn't split), then write some code that puts them back where they belong when your kernel boots.

bzt wrote:
But I'd like to point out that it is very uncommon not to load the kernel at fixed address, therefore chances are good no relocation needed no matter the executable format (elf, pe, coff, aout etc.).

The linker still requires relocations in order to insert the correct fixed address at the correct location in the binary.

Author:  bzt [ Mon Jan 11, 2021 11:25 am ]
Post subject:  Re: create IDT in Assembly

Octocontrabass wrote:
The linker still requires relocations in order to insert the correct fixed address at the correct location in the binary.
True, unless you use one compilation unit with an ORG directive. So it's safe to say you'll need run-time code, I guess? (I always do it in run-time anyway because I only install ISRs for which the drivers register an IRQ during boot)

Cheers,
bzt

Author:  growlnx [ Tue Jan 12, 2021 10:56 am ]
Post subject:  Re: create IDT in Assembly

Well, then the easiest way to do this directly in the assembly is to imitate the result obtained in C.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/