OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Sep 21, 2021 5:13 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: create IDT in Assembly
PostPosted: Sun Jan 10, 2021 10:12 pm 
Offline
User avatar

Joined: Sun Jan 05, 2020 5:51 pm
Posts: 15
Location: 0.0.0.0
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?


_________________
Regards,
Growlnx.


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Sun Jan 10, 2021 10:33 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3349
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.


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Sun Jan 10, 2021 10:48 pm 
Offline
User avatar

Joined: Sun Jan 05, 2020 5:51 pm
Posts: 15
Location: 0.0.0.0
it will be a elf_i386 binary kernel to be loaded with GRUB.

_________________
Regards,
Growlnx.


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Sun Jan 10, 2021 10:53 pm 
Offline
User avatar

Joined: Sun Jan 05, 2020 5:51 pm
Posts: 15
Location: 0.0.0.0
Any ideas on how to get around this problem without resorting to high level?

_________________
Regards,
Growlnx.


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Mon Jan 11, 2021 7:33 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Mon Jan 11, 2021 10:48 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3349
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.


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Mon Jan 11, 2021 11:25 am 
Offline
Member
Member
User avatar

Joined: Thu Oct 13, 2016 4:55 pm
Posts: 1584
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


Top
 Profile  
 
 Post subject: Re: create IDT in Assembly
PostPosted: Tue Jan 12, 2021 10:56 am 
Offline
User avatar

Joined: Sun Jan 05, 2020 5:51 pm
Posts: 15
Location: 0.0.0.0
Well, then the easiest way to do this directly in the assembly is to imitate the result obtained in C.

_________________
Regards,
Growlnx.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group