Skip to contentSkip to conversation

Page protection and i386 cmpxchg8b

Qemu-devel Ilya Shar 2 participants 6 messages Feb 23, 2007 – Feb 27, 2007

lore
Share & tools
lore

Ilya Shar

author
#0 lore raw

Hi,

I'm running i386-darwin-usrer on i386 and some apps (Safari browser) crash because cmpxchg8b attempts to wrie to a qemu-allocated page which is readable but write-protected. When I comment out mprotect in exec.c

// mprotect(g2h(page_addr),
qemu_host_page_size,
// (prot & PAGE_BITS) & ~PAGE_WRITE);

I can proceed further. I would guess mprotect is there for a reason so it doesn't seem like a good solution, besides the runs get substantially slower without mprotect. Is there a solution to this problem or a way to better understand what is going on?

Thanks,
Ilya

____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367

#1 lore raw

Hi Ilya!

Quoted 5 lines · Ilya Shar

I'm running i386-darwin-usrer on i386 and some apps (Safari browser) crash because cmpxchg8b attempts to wrie to a qemu-allocated page which is readable but write-protected. When I comment out mprotect in exec.c

Are you sure it does Safari does crash because of that call? I have the Apple Bug Reporter which complains about the fact that qemu gets a EXC_BAD_ACCESS, but then I get this error:
qemu: Unsupported mach syscall: -61(0xffffffc3) (= semaphore_signal_trap)
or
qemu: Unsupported mach syscall: -33(0xffffffdf) (= syscall_thread_switch)

To fix this we have to implement those syscalls.

Quoted 9 lines

// mprotect(g2h(page_addr),
qemu_host_page_size,
// (prot & PAGE_BITS) & ~PAGE_WRITE);

I can proceed further. I would guess mprotect is there for a reason so it doesn't seem like a good solution, besides the runs get substantially slower without mprotect. Is there a solution to this problem or a way to better understand what is going on?

I think the idea behind the mprotect is to make sure that any changes to this pages gets monitored, and that the tb can be invalidated if the code was modified (self modify-ing code).

Pierre.

author
#2 lore raw

--- Pierre d'Herbemont <[email protected]> wrote:
Hi Pierre,

Thanks for your reply - please see comments inserted below:

Quoted 24 lines

Hi Ilya!

Quoted 1 line · Ilya Shar

I'm running i386-darwin-usrer on i386 and some

apps

Quoted 1 line

(Safari browser) crash because cmpxchg8b attempts

to

Quoted 1 line

wrie to a qemu-allocated page which is readable

but

Quoted 2 lines

write-protected. When I comment out mprotect in
exec.c

Are you sure it does Safari does crash because of that call? I have
the Apple Bug Reporter which complains about the fact that qemu gets
a EXC_BAD_ACCESS, but then I get this error:
qemu: Unsupported mach syscall: -61(0xffffffc3) (= semaphore_signal_trap)
or
qemu: Unsupported mach syscall: -33(0xffffffdf) (= syscall_thread_switch)

Quoted 2 lines

To fix this we have to implement those syscalls.

Sure. At first I was hitting unsupported mach syscalls, so I modified darwin-user/syscall.h according to /Developer/SDKs/MacOSX10.3.9.sdk/usr/include/mach/syscall_sw.h :

$ diff syscall.c syscall.c.orig 
458,465d457
<     case -33:
<         DPRINTF("semaphore_signal_trap(0x%x)\n",
arg1);
<         ret = semaphore_signal_trap(arg1);
<         break;
<     case -34:
<         DPRINTF("semaphore_signal_all_trap(0x%x)\n",
arg1);
<         ret = semaphore_signal_all_trap(arg1);
<         break;
471,474d462
<     case -37:
<         DPRINTF("semaphore_wait_signal_trap(0x%x,
0x%x)\n", arg1, arg2);
<         ret = semaphore_wait_signal_trap(arg1,arg2);

< break;

With this Sfari went past the unsupported call -33 and now stops in call -61 (syscall_thread_switch). Can I just modify syscalls.c in a similar way to fix it?

But a really alarming thing happens before it gets there. If my ethernet cable is not plugged in, cmpxchg8b write to a nonwritable page brings my system down. I suppose it happens in somewhere in the drivers.

...

Quoted 6 lines

I think the idea behind the mprotect is to make sure that any changes
to this pages gets monitored, and that the tb can be invalidated if
the code was modified (self modify-ing code).

That makes sense. Still I am not sure why cmpxchg8b causes problems.

Thanks!
Ilya

Quoted 8 lines

Pierre.

_______________________________________________
Qemu-devel mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/qemu-devel

____________________________________________________________________________________
Looking for earth-friendly autos?
Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
http://autos.yahoo.com/green_center/

#3 lore raw
Quoted 24 lines · Ilya Shar

Sure. At first I was hitting unsupported mach syscalls, so I modified darwin-user/syscall.h according to /Developer/SDKs/MacOSX10.3.9.sdk/usr/include/mach/syscall_sw.h :

$ diff syscall.c syscall.c.orig
458,465d457
<     case -33:
<         DPRINTF("semaphore_signal_trap(0x%x)\n",
arg1);
<         ret = semaphore_signal_trap(arg1);
<         break;
<     case -34:
<         DPRINTF("semaphore_signal_all_trap(0x%x)\n",
arg1);
<         ret = semaphore_signal_all_trap(arg1);
<         break;
471,474d462
<     case -37:
<         DPRINTF("semaphore_wait_signal_trap(0x%x,
0x%x)\n", arg1, arg2);
<         ret = semaphore_wait_signal_trap(arg1,arg2);
Quoted 1 line

< break;

cvs diff -u would be easier to read for me. (or diff -u). You could send this patch to the qemu-devel, that would be cool.

Quoted 3 lines

With this Sfari went past the unsupported call -33 and now stops in call -61 (syscall_thread_switch). Can I just modify syscalls.c in a similar way to fix it?

Yes you can!

Quoted 5 lines

But a really alarming thing happens before it gets there. If my ethernet cable is not plugged in, cmpxchg8b write to a nonwritable page brings my system down. I suppose it happens in somewhere in the drivers.

Ouch! I have noticed the same: qemu can trigger bugs really easily at the kernel level :( Could you explain how you know that cmpxchg8b is the key to our problem? Also qemu signal handlers might be overridden by some mach calls, that could explain the problem you are encountering. We need to work on this.

Pierre.

author
#4 lore raw
Quoted 6 lines · --- Pierre d'Herbemont

...
cvs diff -u would be easier to read for me. (or diff -u). You could
send this patch to the qemu-devel, that would be cool.

Pierre,

Please see the diff file attached.

Quoted 6 lines

...
Ouch! I have noticed the same: qemu can trigger bugs really easily at
the kernel level :( Could you explain how you know that cmpxchg8b is
the key to our problem?

Stepping in gdb I saw that cmpxchg8b attempts to write to a protected page. Besides, when the system crashes, here's what I see in the crash log in the Console app:

...

Exception:  EXC_BAD_ACCESS (0x0001)
Codes:      KERN_PROTECTION_FAILURE (0x0002) at

0x2111faf5

Thread 0 Crashed:
0 qemu-i386 0x0e02254b stq_le_p + 50
(cpu-all.h:344)
1 qemu-i386 0x0e0224d6 helper_cmpxchg8b + 192
(helper.c:1523)
2 <<00000000>> 0x0e1361b4 code_gen_buffer +
708980

3   qemu-i386   0x0e000db3 cpu_loop + 23 (main.c:498)
4   qemu-i386   0x0e001f75 main + 2814 (main.c:917)
5   qemu-i386   0x0e000a8e _start + 216
6   qemu-i386   0x0e0009b5 start + 41

...

Any suggestions how to fix this?

Thanks for your help!
Ilya

Quoted 8 lines

Also qemu signal handlers
might be overridden
by some mach calls, that could explain the problem you are
encountering. We need to work on this.

Pierre.

____________________________________________________________________________________
Food fight? Enjoy some healthy debate
in the Yahoo! Answers Food & Drink Q&A.
http://answers.yahoo.com/dir/?link=list&sid=396545367

#5 lore raw
Quoted 1 line · Ilya Shar

Please see the diff file attached.

I meant send a diff to the qemu-devel in a new mail with [Patch] in the subject so that it can be committed :-)

However, at second thought, I think that it might need some re-work, see [1], and the arguments might need to be byte-swapped.

Quoted 1 line

Any suggestions how to fix this?

As I said before, I think that the problem appears because:

Quoted 1 line
Quoted 1 line

qemu signal handlers might be overridden by some mach calls, that could explain the problem you are encountering. We need to work on this.

That's really not easy to fix, in two words. [1] I think it should consist in writing a custom implementation of semaphore_signal_trap, semaphore_signal_thread_trap, semaphore_wait_signal_trap, etc. that hook into signal.c. Tell me if you want more info on this.

Pierre.