0

I wrote a while loop that hangs when the keyboard controller is not ready:

while(inb(0x64)!=0){

  }

However now qemu restarts everytime I attempt to boot the kernel. Sometimes I can see the vga output for a really short time. I tried to remove the inb, and it still restarts unless I remove it

EDIT:

Here's the complete code:

int *resolution_ptr;


void init_vbe(){
    regs16_t vberegister;
  vberegister.bx=0x4107;
  vberegister.ax=0x4f02;
  int32(0x10,&vberegister);


  *resolution_ptr=1280;
}

void kernel_main(void) 
{

  resolution_ptr=(int*)0x2ff0; /*define the address of the resolution, so that the printstring function can draw the letter correctly*/

  init_vbe ();
    printstring("Kernel sucessfully loaded.");


  while(inb(0x64)!=0){ /*code that cause qemu to restart*/
  }

}

bochs thrown this error when I try to run my kernel in it:

00096454180e[CPU0  ] exception(): 3rd (13) exception with no resolution, shutdown status is 00h, resetting

Also INT32 is from here:https://gist.github.com/carlosascari/35dba95ee3118ebf61f4bd1625f4fa11

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Clement Poon
  • 79
  • 1
  • 6
  • Just to make sure. This is C and not C++? How do you define `inb`? Did you enable interrupts before you completed setting up and loading an IDT? There really isn't much to go on here without a [mcve]. – Michael Petch Jun 02 '20 at 09:13
  • This is C. here's the definition of inb: static __inline unsigned char inb (unsigned short int port) { unsigned char _v; __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (port)); return _v; } (Sorry about the formatting problem) – Clement Poon Jun 02 '20 at 09:45
  • The `inb` is fine. Your problem isn't the loop. You really need to show us all your code and if you wrote your own bootloader show that too. Your question doesn't have enough info to help you. You generally should test specific bits of port 0x64 to see if it is read (usually bits 0 and 1) and that depends on whether you are waiting for the controller to receive data or send it. – Michael Petch Jun 02 '20 at 09:53
  • that loop is almost certainly not causing the machine to reboot. When `kernel_main` returns what do you do? (I mean what does the caller of `kernel_main` do? Does it go in an infinite loop to avoid executing semi random data in memory? – Michael Petch Jun 02 '20 at 10:14
  • 1
    Looking at the `int32` function you link to it does `sti ; enable interrupts` before it returns. Change that to `cli` and what happens? Looks like my suggestion about enabling interrupts before loading an DIT (interrupt descriptor table) could be a problem. Without one the first interrupts that occurs will likely triple fault the machine and make it reboot. – Michael Petch Jun 02 '20 at 10:24
  • nothing happens when I change the sti to cli. Also if I try to use hlt the machine reboots too, and when kernel_main returns it go to a infinite hlt loop. – Clement Poon Jun 02 '20 at 11:11
  • ok here's the github repo:https://github.com/clementtttttttt/UtopiaOS (sorry if the code is insanely messy) – Clement Poon Jun 02 '20 at 11:57
  • keyboard.h and keyboard.asm are missing . `inb` doesn't seem to be defined anywhere (missing?) – Michael Petch Jun 02 '20 at 12:02
  • Anyway, unless there is a bug in the font / print routines the only issue is that rmode.asm is enabling interrupts when you haven't defined any. – Michael Petch Jun 02 '20 at 12:12
  • all missing files are re-added. – Clement Poon Jun 02 '20 at 12:17
  • I was able to compile it and run it and it failed for the reason I said. `rmode.asm` is enabling interrupts but you haven't defined any. First itnerrupt that happens when you are in that loop triple faults and reboots the machine. Changing that STI to CLI fixed it for me, but that isn't a good solution. Modify `int32` to save the interrupt state with `pushf` at the beginning and restore it at end with `popf` and remove the `sti`. I have put that revised code here: https://pastebin.com/7wqJVhW1 – Michael Petch Jun 02 '20 at 12:26
  • now int32 doesn't work. – Clement Poon Jun 02 '20 at 12:47

1 Answers1

0

Works now by removing sti in int32.

Clement Poon
  • 79
  • 1
  • 6
  • 1
    There's no `sti` in the question, therefore it's not a [mcve] of the problem. (Links aren't sufficient). Probably not worth editing the question, though; unlikely future readers could find it by searching even if they were having a similar problem. Glad you were able to find the problem in your own code, though. – Peter Cordes Jun 02 '20 at 13:01