Page 1 of 1

create IDT in Assembly

Posted: Sun Jan 10, 2021 10:12 pm
by growlnx
Hello everybody!

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

Code: Select all

%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?

Re: create IDT in Assembly

Posted: Sun Jan 10, 2021 10:33 pm
by Octocontrabass
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.

Re: create IDT in Assembly

Posted: Sun Jan 10, 2021 10:48 pm
by growlnx
it will be a elf_i386 binary kernel to be loaded with GRUB.

Re: create IDT in Assembly

Posted: Sun Jan 10, 2021 10:53 pm
by growlnx
Any ideas on how to get around this problem without resorting to high level?

Re: create IDT in Assembly

Posted: Mon Jan 11, 2021 7:33 am
by bzt
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: Select all

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

Re: create IDT in Assembly

Posted: Mon Jan 11, 2021 10:48 am
by Octocontrabass
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.

Re: create IDT in Assembly

Posted: Mon Jan 11, 2021 11:25 am
by bzt
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

Re: create IDT in Assembly

Posted: Tue Jan 12, 2021 10:56 am
by growlnx
Well, then the easiest way to do this directly in the assembly is to imitate the result obtained in C.