Author: ed
Date: Thu Dec 21 09:21:40 2017
New Revision: 327057
URL: https://svnweb.freebsd.org/changeset/base/327057

Log:
  Make truss work for CloudABI executables on i386.
  
  The system call convention is different from i386 binaries running on
  FreeBSD/amd64, but this is not noticeable by executables. On
  FreeBSD/amd64, the vDSO already does padding of arguments and return
  values to 64-bit values. On i386, it does not, meaning that system call
  return values are simply stored in registers.

Added:
  head/usr.bin/truss/i386-cloudabi32.c
     - copied, changed from r327055, head/usr.bin/truss/amd64-cloudabi32.c
Modified:
  head/usr.bin/truss/Makefile

Modified: head/usr.bin/truss/Makefile
==============================================================================
--- head/usr.bin/truss/Makefile Thu Dec 21 09:17:48 2017        (r327056)
+++ head/usr.bin/truss/Makefile Thu Dec 21 09:21:40 2017        (r327057)
@@ -18,6 +18,7 @@ ABIS+=                cloudabi64
 .endif
 .if ${MACHINE_CPUARCH} == "i386"
 ABIS+=         i386-linux
+ABIS+=         cloudabi32
 .endif
 .if ${MACHINE_CPUARCH} == "amd64"
 ABIS+=         amd64-linux

Copied and modified: head/usr.bin/truss/i386-cloudabi32.c (from r327055, 
head/usr.bin/truss/amd64-cloudabi32.c)
==============================================================================
--- head/usr.bin/truss/amd64-cloudabi32.c       Thu Dec 21 04:23:00 2017        
(r327055, copy source)
+++ head/usr.bin/truss/i386-cloudabi32.c        Thu Dec 21 09:21:40 2017        
(r327057)
@@ -38,7 +38,7 @@ __FBSDID("$FreeBSD$");
 #include "truss.h"
 
 static int
-amd64_cloudabi32_fetch_args(struct trussinfo *trussinfo, unsigned int narg)
+i386_cloudabi32_fetch_args(struct trussinfo *trussinfo, unsigned int narg)
 {
        struct current_syscall *cs;
        struct ptrace_io_desc iorequest;
@@ -46,7 +46,7 @@ amd64_cloudabi32_fetch_args(struct trussinfo *trussinf
        lwpid_t tid;
 
        if (narg > 0) {
-               /* Fetch registers, containing the address of the arguments. */
+               /* Fetch registers, containing the stack pointer. */
                tid = trussinfo->curthread->tid;
                if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) == -1) {
                        fprintf(trussinfo->outfile,
@@ -54,10 +54,10 @@ amd64_cloudabi32_fetch_args(struct trussinfo *trussinf
                        return (-1);
                }
 
-               /* Fetch arguments. They are already padded to 64 bits. */
+               /* Fetch arguments. */
                cs = &trussinfo->curthread->cs;
                iorequest.piod_op = PIOD_READ_D;
-               iorequest.piod_offs = (void *)regs.r_rcx;
+               iorequest.piod_offs = (void **)regs.r_esp + 1;
                iorequest.piod_addr = cs->args;
                iorequest.piod_len = sizeof(cs->args[0]) * narg;
                if (ptrace(PT_IO, tid, (caddr_t)&iorequest, 0) == -1 ||
@@ -68,45 +68,31 @@ amd64_cloudabi32_fetch_args(struct trussinfo *trussinf
 }
 
 static int
-amd64_cloudabi32_fetch_retval(struct trussinfo *trussinfo, long *retval,
+i386_cloudabi32_fetch_retval(struct trussinfo *trussinfo, long *retval,
     int *errorp)
 {
-       struct ptrace_io_desc iorequest;
        struct reg regs;
        lwpid_t tid;
 
-       /* Fetch registers, containing the address of the return values. */
        tid = trussinfo->curthread->tid;
        if (ptrace(PT_GETREGS, tid, (caddr_t)&regs, 0) == -1) {
                fprintf(trussinfo->outfile, "-- CANNOT READ REGISTERS --\n");
                return (-1);
        }
 
-       if (regs.r_rax == 0) {
-               /* System call succeeded. Fetch return values. */
-               iorequest.piod_op = PIOD_READ_D;
-               iorequest.piod_offs = (void *)regs.r_rcx;
-               iorequest.piod_addr = retval;
-               iorequest.piod_len = sizeof(retval[0]) * 2;
-               if (ptrace(PT_IO, tid, (caddr_t)&iorequest, 0) == -1 ||
-                   iorequest.piod_len == 0)
-                       return (-1);
-               *errorp = 0;
-       } else {
-               /* System call failed. Set error. */
-               retval[0] = regs.r_rax;
-               *errorp = 1;
-       }
+       retval[0] = regs.r_eax;
+       retval[1] = regs.r_edx;
+       *errorp = (regs.r_eflags & PSL_C) != 0;
        return (0);
 }
 
-static struct procabi amd64_cloudabi32 = {
+static struct procabi i386_cloudabi32 = {
        "CloudABI ELF32",
        SYSDECODE_ABI_CLOUDABI32,
-       amd64_cloudabi32_fetch_args,
-       amd64_cloudabi32_fetch_retval,
-       STAILQ_HEAD_INITIALIZER(amd64_cloudabi32.extra_syscalls),
+       i386_cloudabi32_fetch_args,
+       i386_cloudabi32_fetch_retval,
+       STAILQ_HEAD_INITIALIZER(i386_cloudabi32.extra_syscalls),
        { NULL }
 };
 
-PROCABI(amd64_cloudabi32);
+PROCABI(i386_cloudabi32);
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to