Page 1 of 1

Hardware Interrupts not working after switch to ring 3

Posted: Tue Feb 11, 2014 2:16 am
by teenHack42
I know I did not get off well in this forum but I will try...

I was working on sleeping using the PIT(sleep works when tasking and user mode are disabled) but found that 1. my PIT was not firing and 2. None of my hardware interrupts where firing(after user mode(ring 3)).

I have searched around for similar problems and found none :) .

My kernel is based of JamesM's tutorial and has been read through half a dozen times to find possible fixes.

I know that my interrupts AND my PIT work before user mode.

I think you want to look at my "switch_to_user_mode" function.

Code: Select all

void switch_to_user_mode()
{
	// Set up our kernel stack.
	set_kernel_stack(current_task->kernel_stack+KERNEL_STACK_SIZE);
	
	// 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 %esp; \
	  pushf; \
	  pop %eax; \
	  or %eax, 0x200 ; \
	  push %eax ; \
	  pushl $0x1B; \
	  push $1f; \
	  iret; \
	1: \
	  "); 
	  
}
if you would like me to post any other code please ask and if youwould like to look at the whole picture please visit my https://github.com/teenHack42/MatrixOS

PS. how do I make a link under a word or something like that?

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Feb 11, 2014 2:23 am
by VolTeK
teenHack42 wrote:I think you want to look at my "switch_to_user_mode" function
If you know where the problem is, focus more there. An "Aha" moment for you, will help you hours more down the road.


You might be more surprised to find that the problem may not even be there.

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Feb 11, 2014 2:27 am
by teenHack42
VolTeK wrote:You might be more surprised to find that the problem may not even be there.
Are you suggesting something? :shock:

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Feb 11, 2014 2:45 am
by bluemoon
The user mode function seems good, have you properly ack the PIC?

By the way, in the tutorial:

Code: Select all

     pop %eax
     or %eax, 0x200
This is good for the tutorial since the user mode code is a continue of flow from kernel.
However for practical you would be starting new process, which you want a defined state and it's better to do mov eax, 0x0202 instead.
teenHack42 wrote:PS. how do I make a link under a word or something like that?
Read the manual of phpBB.

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Feb 11, 2014 2:20 pm
by Gigasoft
The cause of your problem is not clear from the posted code. Perhaps some memory is being overwritten somewhere (for example, if you are already using kernel_stack as your stack).

Either way, there is a possible huge security flaw waiting to happen here. If set_kernel_stack is not inline, another thread can redirect execution by overwriting its return address before it returns. And if you eventually want MP support, a thread executing on a different CPU can overwrite the segment that is returned to by the iret instruction. The kernel should always use its own stack, inaccessible by user mode code. So, instead of "switching" to user mode, you should have a function that "calls" an user mode function, and perhaps another function (available by a system call) that returns to where you originally left off.

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Jun 07, 2016 3:49 am
by sajadbanooie
same thing for me.
when I try to modify the pushed eflags and enable interrupt flag Bochs panics with the message "APIC write at unaligned address 0xfee00ffc."

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Jun 07, 2016 4:27 am
by iansjack
It looks as if you are corrupting your stack. Read this thread for discussion of the error: http://forum.osdev.org/viewtopic.php?f=1&p=177970

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Jun 07, 2016 5:24 am
by sajadbanooie
it's fixed now.
interrupts and irqs working correctly in ring 3.
the problem was with my tss's esp0.

Re: Hardware Interrupts not working after switch to ring 3

Posted: Tue Jun 07, 2016 1:07 pm
by mariuszp
I don't recommend following his tutorial unless you really know what you are doing.
http://wiki.osdev.org/James_Molloy's_Tu ... Known_Bugs