Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 21 Aug 2007 22:53:23 +0400
From:      "Yuriy Tsibizov" <yuriy.tsibizov@gmail.com>
To:        freebsd-hackers@freebsd.org
Cc:        freebsd-emulation@freebsd.org
Subject:   User-mode Linux (Was: modify syscall nr on-the-fly)
Message-ID:  <c019b3090708211153o7dce8365l945b24ad1c962d22@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
(replying to myself)


2007/8/21, Yuriy Tsibizov <yuriy.tsibizov@gmail.com>:
> 2007/8/20, Kostik Belousov <kostikbel@gmail.com>:
> > On Sat, Aug 18, 2007 at 02:01:26PM +0400, Yuriy Tsibizov wrote:
> > > I'm trying to get user-mode Linux to run under FreeBSD Linux emulation (on
> > > i386).
> > >
> > > User-mode Linux in it's start-up tests tries to modify syscall number (to be
> > > called by kernel) on-the-fly
> > > (http://fxr.watson.org/fxr/source/arch/um/os-Linux/start_up.c?v=linux-2.6).
> > > It forks a child thread that stops
> > > (using SIGSTOP), calls getpid() (that will be intercepted by parent thread
> > > using PTRACE_SYSCALL)
> > > and return some value based on getpid() results. Main thread waits for
> > > SIGSTOP in child process and
> > > enables PTRACE_SYSCALL (I have some code that implements it. It makes some
> > > incompatible changes
> > > to PT_SYSCALL that will break FreeBSD applications, but works for Linux
> > > apps). When main thread
> > > catches SIGTRAP (generated by ptrace) it tries to modify EAX of child thread
> > > (with PTRACE_PEEKUSR
> > > and PTRACE_POKEUSR) to replace getpid syscall with getppid.
> > >
> > > is it possible to get updated EAX (and other registers as well)  in
> > > syscall(...) after ptracestop(...) in PTRACESTOP_SC(...) returns?
> > >
> > > Hope for your help,
> > >
> > > Yuriy.
> >
> > If I understand right what you want, I doubt that existing code would
> > allow you to change syscall number in debugger process for debuggee.
> > You shall look at the sys/i386/i386/trap.c, syscall() function [adjust
> > as needed for other arches]. It calculates callp before doing PTRACESTOP_SC,
> > as well as copies the syscall arguments into the kernel address space.
>
> Yes, I know this. I'm going to recalculate callp after PTRACESTOP_SC.
> And, there will be no need to copyin from user space -- all syscalls
> parameters are passed in registers (it will be used only for processes
> running under Linux emulation).
Updated registers are available via *frame.

With some hacks (some return codes needed by user-mode Linux are
hardcoded into kernel) it loads:

Core dump limits :
	soft - NONE
	hard - NONE
Checking that ptrace can change system call numbers...OK
Checking syscall emulation patch for ptrace...missing
Checking for tmpfs mount on /dev/shm...nothing mounted on /dev/shm
Checking PROT_EXEC mmap in /tmp/...OK
Checking for the skas3 patch in the host:
  - /proc/mm...  - PTRACE_FAULTINFO...  - PTRACE_LDT...UML running in SKAS0 mode
Linux version 2.6.22-rc2 (jdike@tp.user-mode-linux.org) (gcc version
4.1.1 20070105 (Red Hat 4.1.1-51)) #342 Wed May 23 11:56:49 EDT 2007
Built 1 zonelists.  Total pages: 8128
Kernel command line: root=98:0
PID hash table entries: 128 (order: 7, 512 bytes)
Dentry cache hash table entries: 4096 (order: 2, 16384 bytes)
Inode-cache hash table entries: 2048 (order: 1, 8192 bytes)
Memory: 30288k available
Mount-cache hash table entries: 512
Checking for host processor cmov support...Yes
Checking for host processor xmm support...No
openpty failed, errno = 22
openpty failed, errno = 22
aio_thread failed to initialize context, err = 38
2.6 AIO not supported on the host - reverting to 2.4 AIO
2.6 host AIO support not used - falling back to I/O thread
NET: Registered protocol family 16
NET: Registered protocol family 2
IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
TCP established hash table entries: 1024 (order: 1, 8192 bytes)
TCP bind hash table entries: 1024 (order: 0, 4096 bytes)
TCP: Hash tables configured (established 1024 bind 1024)
TCP reno registered
Checking host MADV_REMOVE support...OK
os_set_fd_async : Failed to fcntl F_SETOWN (or F_SETSIG) fd 6 to pid
1191, errno = 22
Failed to get IRQ for management console
os_set_fd_async : Failed to fcntl F_SETOWN (or F_SETSIG) fd 8 to pid
1191, errno = 22
um_request_irq failed - errno = 22
Host TLS support detected
Detected host type: i386
VFS: Disk quotas dquot_6.5.1
Dquot-cache hash table entries: 1024 (order 0, 4096 bytes)
io scheduler noop registered
io scheduler anticipatory registered (default)
io scheduler deadline registered
io scheduler cfq registered
TCP cubic registered
NET: Registered protocol family 1
NET: Registered protocol family 17
Initialized stdio console driver
Console initialized on /dev/tty0
Initializing software serial port version 1
Couldn't stat "root_fs" : err = 2
Failed to initialize ubd device 0 :Couldn't determine size of device's file
VFS: Cannot open root device "98:0" or unknown-block(98,0)
Please append a correct "root=" boot option; here are the available partitions:
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(98,0)

EIP: 0033:[<28093021>] CPU: 0 Not tainted ESP: 003b:28068fc4 EFLAGS: 00000246
    Not tainted
EAX: 00000000 EBX: 000004aa ECX: 00000013 EDX: 000004aa
ESI: 000004a7 EDI: 00000000 EBP: 28068fd8 DS: 003b ES: 003b
087fce64:  [<08069628>] show_regs+0xb4/0xb9
087fce90:  [<08057ca8>] panic_exit+0x25/0x3f
087fcea4:  [<08078720>] notifier_call_chain+0x21/0x46
087fcec4:  [<080787bb>] __atomic_notifier_call_chain+0x17/0x19
087fcee0:  [<080787d2>] atomic_notifier_call_chain+0x15/0x17
087fcefc:  [<0806daba>] panic+0x52/0xdd
087fcf1c:  [<08049af9>] mount_block_root+0x100/0x116
087fcf70:  [<08049b5b>] mount_root+0x4c/0x54
087fcf94:  [<08049c41>] prepare_namespace+0xde/0x10a
087fcfa4:  [<08049827>] kernel_init+0x79/0x85
087fcfb4:  [<08063a39>] run_kernel_thread+0x37/0x42
087fcfe0:  [<08058095>] new_thread_handler+0x57/0x7e
087fcffc:  [<a55a5a5a>] 0xa55a5a5a

I'll need two more flags in p_stops to add two optios:
- respect PTRACE_(OLD)SETOPTIONS PTRACE_O_TRACESYSGOOD (generate
SIGTRAP | 0x80 instead of plain SIGTRAP)
- use Linux PTRACE_SYSCALL conventions (clear S_PT_SCE and S_PT_SCX in
PTRACESTOP_SC)
to make it more than just a set of hacks to run single program.

PTRACE_(PEEK|POKE)USR seems to need small rewrite too.


Yuriy.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?c019b3090708211153o7dce8365l945b24ad1c962d22>