Page 1 of 1
Keyboard works in kernel mode but not user mode
Posted: Fri May 27, 2011 8:56 pm
by casnix
I can read the keyboard input before I switch to usermode but not after.
If you could take a look at my kernel code (:P) this doesn't make sense to me, unless I need to create system calls to the functions that a system call'd function calls...
You won't be able to run a make of the code 'cause I can't upload an archive big enough to hold the initrd and disk image an' I had to split the archive in half to be under 64kb
Thanks, Matt
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 3:40 am
by thepowersgang
(I haven't checked the code yet)
Are you trying to directly access the Keyboard controller from user mode? How does it fail?
Typically (unless allowed by the IO bitmap in the TSS) user mode code cannot use IO ports (in*/out*). You need to have syscalls to abstract access to the keyboard (usually through a concept of a terminal)
Please give us more information on _how_ it fails, and where (and don't just upload your entire source tree and expect us to just debug it for you)
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 9:59 am
by mariuszp
Try implementing a keyboard buffer and then you can have a getch() function that returns a character to userspace. By the way, userspace code can never run kernel code with just a call/jmp instruction. You need something like an interrupt-based system calls interface. More about that on the wiki, just search for 'system calls'.
Your keyboard code (kb.c?) also seems to talk to the user too much. If I press CTRL+ALT+DELETE I don't expect the kernel to say "Just press the power button.", especially if I'm in a GUI. Just a tip
P.S. Can you give us some userspace code for interfacing the keyboard? If I only see
kernel code I cannot help you with
userspace code. OK?
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 12:07 pm
by casnix
maybe i should have mentioned this:
I have system calls,
a successful gets and getch function (which works in kernel mode).
I have an interrupt based syscall system, which is why I don't understand why it's not working. Is it possible that I have to send a reset command to the KBC after i switch to user mode
?
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 12:16 pm
by casnix
mariuszp wrote:Try implementing a keyboard buffer and then you can have a getch() function that returns a character to userspace. By the way, userspace code can never run kernel code with just a call/jmp instruction. You need something like an interrupt-based system calls interface. More about that on the wiki, just search for 'system calls'.
Your keyboard code (kb.c?) also seems to talk to the user too much. If I press CTRL+ALT+DELETE I don't expect the kernel to say "Just press the power button.", especially if I'm in a GUI. Just a tip
P.S. Can you give us some userspace code for interfacing the keyboard? If I only see
kernel code I cannot help you with
userspace code. OK?
About the CTRL+ALT+DEL I'm just leaving it at that until later when I come up with a good kernel reset (basically call main(), but yeah).
In my syscall.h and syscall.c I'm using the same syscall system as in JamesM's tutorial, so the functions kputs, kputc, kclear_screen, kgets...etc.... are all integer returning functions which cause an interrupt to int 0x90. So there is userpace code after the switch_to_user_mode() call.
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 12:22 pm
by mariuszp
OK, but how exactly does getch() not work? Does it never return? Does it return something not expected? Does it cause an exception (interrupt)? We need info!
P.S.
Code: Select all
/* I really don't know what to do yet with Alt, Ctrl, etc. -- punt */
return temp;
Another tip: Alt is used by some keyboard for national characters. For example, I have a polish keyboard set up, so when I press Alt+A I get Ä„.
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 12:41 pm
by casnix
when in user mode getch doesnt return. I'm not sure the IRQ1 is firing, let me check that. And that's a good idea for the Alt. How would I implement unicode into text mode though?Assuming its unicode
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 12:46 pm
by mariuszp
Ah, I see what's wrong!!!!
The fact that it only works in kernel is probably rather accidental and has nothing to do with the problem. Your getch() functions read the character from port 0x60, but this should only be done in the interrupt handler only. Try something like this:
Code: Select all
void handler()
{
write_to_buffer(convert(inb(0x60)));
};
int getch()
{
return read_from_buffer();
};
This is just proto-code, but you get the idea.
Re: Keyboard works in kernel mode but not user mode
Posted: Sat May 28, 2011 12:49 pm
by mariuszp
casnix wrote:when in user mode getch doesnt return. I'm not sure the IRQ1 is firing, let me check that. And that's a good idea for the Alt. How would I implement unicode into text mode though?Assuming its unicode
You don't have to use unicode. There's something here:
http://en.wikipedia.org/wiki/ISO/IEC_8859-13
As to using it in text mode, you can try uploading a font to VGA (i don't know how to), which has the characters in the above encoding. But that would only work for lithuanian, litvian, estonian and polish, so I'd say wait until you make a GUI
For example, if you use that encoding, you can make the 'nose A' I showed you as character 192 in your font.