in my operating system I need to move to user mode. I know how to make the move but when I'm in user mode, there are no interrupts. so that means for example:
when I type something in keyboard, the buffer won't fill. so how do I suppose to make system calls to get char from keyboard if the buffer is empty?
If you have some example I can look on system calls and can tell me how I have to make it so I can be in user mode and still get input from keyboard, I would appreciate it.
Hope I make it clear.. Thanks for your help!
Developing an os, user mode, interrupts and syscalls
-
- Posts: 5
- Joined: Mon Feb 25, 2019 12:47 pm
Re: Developing an os, user mode, interrupts and syscalls
What makes you think that interrupts don't happen when running user-mode tasks? The clue is in the name - "interrupt".
-
- Member
- Posts: 71
- Joined: Fri Jun 28, 2013 1:48 am
- Contact:
Re: Developing an os, user mode, interrupts and syscalls
Interrupts also fire under user mode, as long as you set rflags.IF=1 before iret.
When process tries to read from an empty file, the process will change to pending state. And that process will be removed from ready queue. When other process or ISR write to the file, making that file not empty, the pending process can be wake up.
So when process calls getchar() while the stdin is empty, the process is actually pending, i.e. not running on CPU. When stdin becomes not empty, the process resumes and function getchar() returns.
When process tries to read from an empty file, the process will change to pending state. And that process will be removed from ready queue. When other process or ISR write to the file, making that file not empty, the pending process can be wake up.
So when process calls getchar() while the stdin is empty, the process is actually pending, i.e. not running on CPU. When stdin becomes not empty, the process resumes and function getchar() returns.
Reinventing the Wheel, code: https://github.com/songziming/wheel
-
- Posts: 5
- Joined: Mon Feb 25, 2019 12:47 pm
Re: Developing an os, user mode, interrupts and syscalls
When you move to user mode you have the command "cli" which disable all interruptsiansjack wrote:What makes you think that interrupts don't happen when running user-mode tasks? The clue is in the name - "interrupt".
-
- Posts: 5
- Joined: Mon Feb 25, 2019 12:47 pm
Re: Developing an os, user mode, interrupts and syscalls
So when I have this code in c which move me to user mode:songziming wrote:Interrupts also fire under user mode, as long as you set rflags.IF=1 before iret.
When process tries to read from an empty file, the process will change to pending state. And that process will be removed from ready queue. When other process or ISR write to the file, making that file not empty, the pending process can be wake up.
So when process calls getchar() while the stdin is empty, the process is actually pending, i.e. not running on CPU. When stdin becomes not empty, the process resumes and function getchar() returns.
Code: Select all
void switch_to_user_mode()
{
// Set up a stack structure for switching to user mode.
asm volatile(" \
cli; \
mov $0x23, %ax; \
mov %ax, %ds; \
mov %ax, %es; \
mov %ax, %fs; \
mov %ax, %gs; \
\
mov %esp, %eax; \
pushl $0x23; \
pushl %eax; \
pushf; \
pushl $0x1B; \
push $1f; \
iret; \
1: \
");
}
Re: Developing an os, user mode, interrupts and syscalls
Add an "orl $0x200, (%esp)" line after "pushf" in the assembly code. That will set IF in the flags value that is loaded by the IRET instruction.lidorelias3 wrote: How do I set IF=1 So I still have interrupts in user mode and the system won't crash when I access the cpu?
I don't know what you mean by "access the cpu", all code "accesses" the CPU; where else would it run?
What do you think is the significance of the fact that the final instruction in the code to switch to usermode is "IRET"? That's the same instruction used to return from an interrupt. If that's how you enter usermode in first place, doesn't that give you a clue about what happens when you return from an interrupt that fired while the CPU was running in ring-3?
Re: Developing an os, user mode, interrupts and syscalls
So - don't use that instruction, and don't disable interrupts.lidorelias3 wrote:When you move to user mode you have the command "cli" which disable all interruptsiansjack wrote:What makes you think that interrupts don't happen when running user-mode tasks? The clue is in the name - "interrupt".