Hardware Interrupts not working after switch to ring 3

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
teenHack42
Posts: 13
Joined: Mon Jan 20, 2014 7:58 pm

Hardware Interrupts not working after switch to ring 3

Post 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?
https://github.com/teenHack42/MatrixOS
Working on: PCI[E]
--
teenHack42
User avatar
VolTeK
Member
Member
Posts: 815
Joined: Sat Nov 15, 2008 2:37 pm
Location: The Fire Nation

Re: Hardware Interrupts not working after switch to ring 3

Post 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.
teenHack42
Posts: 13
Joined: Mon Jan 20, 2014 7:58 pm

Re: Hardware Interrupts not working after switch to ring 3

Post by teenHack42 »

VolTeK wrote:You might be more surprised to find that the problem may not even be there.
Are you suggesting something? :shock:
https://github.com/teenHack42/MatrixOS
Working on: PCI[E]
--
teenHack42
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Hardware Interrupts not working after switch to ring 3

Post 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.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Hardware Interrupts not working after switch to ring 3

Post 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.
sajadbanooie
Posts: 8
Joined: Mon Jun 06, 2016 2:04 am

Re: Hardware Interrupts not working after switch to ring 3

Post 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."
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Hardware Interrupts not working after switch to ring 3

Post 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
sajadbanooie
Posts: 8
Joined: Mon Jun 06, 2016 2:04 am

Re: Hardware Interrupts not working after switch to ring 3

Post by sajadbanooie »

it's fixed now.
interrupts and irqs working correctly in ring 3.
the problem was with my tss's esp0.
mariuszp
Member
Member
Posts: 587
Joined: Sat Oct 16, 2010 3:38 pm

Re: Hardware Interrupts not working after switch to ring 3

Post 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
Post Reply