Keyboard works in kernel mode but not user mode

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Keyboard works in kernel mode but not user mode

Post 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
Attachments
include.zip
the include/ folder
(21.61 KiB) Downloaded 83 times
src.zip
the src/ folder
(45.89 KiB) Downloaded 74 times
You are a computer.
~ MCS ~
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: Keyboard works in kernel mode but not user mode

Post 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)
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Keyboard works in kernel mode but not user mode

Post 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 :D

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?
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Re: Keyboard works in kernel mode but not user mode

Post 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
?
You are a computer.
~ MCS ~
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Re: Keyboard works in kernel mode but not user mode

Post 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 :D

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.
You are a computer.
~ MCS ~
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Keyboard works in kernel mode but not user mode

Post 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 Ä„.
User avatar
casnix
Member
Member
Posts: 67
Joined: Fri Jan 14, 2011 7:24 pm

Re: Keyboard works in kernel mode but not user mode

Post 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
You are a computer.
~ MCS ~
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Keyboard works in kernel mode but not user mode

Post 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.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Keyboard works in kernel mode but not user mode

Post 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 :D

For example, if you use that encoding, you can make the 'nose A' I showed you as character 192 in your font.
Post Reply