0

I have a C function in my custom kernel that prints DEBUG on the top left corner.

I call that function using a different cs :

asm("lcall $0x28, $0x0"); 

(The code is copied at the beggining of the 0x28 code segment).

In the c function that prints the DEBUG message, I return using :

asm("lret;");

But when I execute the code, It triple fault milliseconds after showing the DEBUG message. If I just do :

asm("call $0x08, $0x100"); 

It works. ($0x08 is kernel code covering the whole memory, $0x28 is the 'debug' program code segment starting at 0x100).

I suppose the problem comes from the gcc asm generation as if I execute the same return but from NASM, the "retf" is working and no triple fault.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Leosa99 _
  • 33
  • 6
  • 3
    You can't just stick an asm return into a compiler generated C function. Anyway, look at the assembly listing. – Jester Aug 31 '21 at 13:08
  • 2
    GCC doesn't support non-flat memory models for x86. You need a compiler that does. – zwol Aug 31 '21 at 13:47
  • There is no option or tweak to make gcc produce binary for not flat memory model ? The only option is to use another compiler ? – Leosa99 _ Aug 31 '21 at 13:55
  • 1
    GCC is a portable compiler; memory segmentation would make a lot of things (like how pointers work) fundamentally different and more complicated than other systems, for very little benefit. So no, GCC doesn't support non-flat memory models; its source code is already complicated enough, and support for super weird stuff would introduce more corner cases that slow down development. (And segmentation support would probably get broken frequently because basically nobody would use it; maintaining more ports of GCC is more work.) – Peter Cordes Aug 31 '21 at 16:22
  • 1
    Note that if the debug function will modify any registers, you need to declare them as clobbered in the inline asm statement. And as mentioned you can't dump a return statement into the middle of a C function; there will be a stack frame set up and the stack pointer won't point at the return address. If you want to use gcc to far-call from one function to another, you probably have to write a shim in pure assembly that near-calls the desired the function and `lret`s, then you can far-call the shim. – Nate Eldredge Aug 31 '21 at 16:59

0 Answers0