intermezzOS / kernel

A hobby operating system, in Rust
http://intermezzos.github.io/
Apache License 2.0
1.39k stars 91 forks source link

keyboard handler stops main? #4

Closed steveklabnik closed 7 years ago

steveklabnik commented 8 years ago

If I change src/lib.rs like this:

    let mut i = 0;
    loop {
        kprintln!("i {}", i);
        i = i + 1;
    }

It prints just fine, but when i hit a key, it doesn't resume printing.

WilsonGiese commented 8 years ago

@steveklabnik You may have already figured this out for yourself, but this code is creating an easy opportunity for a deadlock. While looping, that code almost always has the VGA lock, so when the interrupt handler attempts to print the key it also needs to obtain the VGA lock, which it can't, so it spins forever and ever (so sad)

I don't really know how operating systems usually handle keyboard input, but I'd like to learn. Have you already thought about how you wanted to tackle this? If so, I'd love to hear your thoughts :)

emilio commented 8 years ago

@WilsonGiese I'd guess that, just as with signal handlers in POSIX, printing (as anything else that could corrupt global state or hold a global lock) during an interrupt handler is just not safe, and generally not a good idea, and that the way to tackle it would be storing the key code in a buffer for latter use or similar.

That being said I'm not a kernel dev, so happy to be corrected :)

WilsonGiese commented 8 years ago

@emilio My first thought was to use an internal buffer as well, but I don't think this would work well in practice. I don't think you'd ever want to store a key for later use, but rather treat the keyboard interrupt as a keyboard event that could be subscribed to. So maybe the kernels job would only be to deliver keyboard events to its subscribers?

This may not be the right forum for this discussion, but I think it's an interesting design question. There are probably several ways to handle keyboard/mouse input.

steveklabnik commented 8 years ago

@WilsonGiese I had not thought about this issue in a very long time, but yes, I can see now that this is very clearly a deadlock.

Have you already thought about how you wanted to tackle this? If so, I'd love to hear your thoughts :)

I have been working on refactoring the whole interrupt handling system, so I haven't given it a ton of thought yet. Like @emilio , I assumed that storing it in some kind of buffer was probably the way to go, but that's about the extent of it.

I hope to be done with the refactoring pretty soon; it's mostly removing duplication at the moment, so I don't have 256 actual handler functions defined...

steveklabnik commented 7 years ago

Since handlers are gone now, closing this. Of course, when bringing them back, this will be important...