From patchwork Fri Jan 18 21:30:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 156053 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp3730084jaa; Fri, 18 Jan 2019 13:35:33 -0800 (PST) X-Google-Smtp-Source: ALg8bN437lkLfG+fYztUbEfxUBcULNYMkenfv1FtxCwRkS1SVYmjVYDGc43IjUyXoJIGO1uKHRUX X-Received: by 2002:adf:80cf:: with SMTP id 73mr18193003wrl.57.1547847333102; Fri, 18 Jan 2019 13:35:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547847333; cv=none; d=google.com; s=arc-20160816; b=SB3PKC/RmkC0Q04ldCVCk9G8FzByg1x8KxsprMLvSoyKTYQTupu/JjIFcyWxQdaE2c k4bdEXJ/KTJbALaTwYtnaraulQu6OBGaolxENUar5ghVPiiYkqLwFxt3lKtBON6XoqSd UVF+1xoGHLpeuJJkwIVSRfiVb+FRHuVqk3Aew7pTBsT5RHHYE9pJj0tzh15P5Hef6dAU lh16glsDn5faSD2FZ+ezSWH0rNBPNJwCg+g3NTFD4bEzWODZ3V8+ZsHHxn7hD2esS/DA LXG48pOzYcXt4XbcvDJAQAvDUM4FYyo6k49pgpJbo3urAhhXu8ZTc465JZzZF6dVlDRY N/zQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=MwgCZGcEc5lQOM9B1e8no3PfGioWwuL5PT0O9ad7iHA=; b=jxWA1uoq3u655R3rid1gKEfcR+YdDh0D1TsmR34F2uCXb4bSvwaCb9GZIUpceAjObr lij5b/uEYH0qWnGXLaxBNO3JxKCDymx6LgaR3lJo0UKtT56CHU6vOXo94/DZ7q7+N84i cOLfwffjHiN93cuBeGVtPDUqfyUQPDkaBtpXQC/AWHw7r9JoSsjg1vmeRLF7xudlETfP uqNSUtFus1Ky8uX1B6u4XigizYEKpgWRkd6ZXKdijaJcpVt79WnC2VbdNzqaNIsDLCo1 eqgG5IShK3J91O0dNW/Im9tTj0TnU3vINSoCUBGEtI+id7H4dKqKD+sVhL/MqgTQ/zhi 3aPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=j0aFwG1r; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id c13si61167899wrv.233.2019.01.18.13.35.32 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 18 Jan 2019 13:35:33 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=j0aFwG1r; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:47432 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbnX-0007cQ-Ql for patch@linaro.org; Fri, 18 Jan 2019 16:35:31 -0500 Received: from eggs.gnu.org ([209.51.188.92]:55938) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbkI-00053K-HE for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:32:12 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gkbkH-0004Z3-3D for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:32:10 -0500 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]:45027) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gkbkG-0004XX-Qg for qemu-devel@nongnu.org; Fri, 18 Jan 2019 16:32:09 -0500 Received: by mail-pl1-x644.google.com with SMTP id e11so6872945plt.11 for ; Fri, 18 Jan 2019 13:32:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=MwgCZGcEc5lQOM9B1e8no3PfGioWwuL5PT0O9ad7iHA=; b=j0aFwG1rXu9jPT1J0sdoZNvvl5qSzGCy6LNmvuAW9gV2W/qBmS3BLw6XoBEgFJgH1u 45HKkmOCuFnY1C9Wsm5fV9DnfDRQjGR4WInYY9S68eMIONJ5BgM/UYr8wmw8QwTgVnyZ hsHCOEBBJqQ0oEqhJwIDjsZs5obKae04qOhgw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=MwgCZGcEc5lQOM9B1e8no3PfGioWwuL5PT0O9ad7iHA=; b=S+ZEu6on9/g04fLyVwMGc+eRvXyrm7+IFK9ljXETJxuMSzpMAqVKOIWS9lqcLJyFf3 N+WAhyaz58oERVGEs9l92u6L2gkloTDsIDAqopmmvDrY4eu+Xg7bp1FwQnWBDbrJdBo4 VocwohMCB1H3xt+boD4+/qLyWT9+iQa9rb8IetmZEPbaabcz/+Lnq09/C6g/3vQQtrzr mXehBuc+L8hnt2r/WlSpgHbxbfdvgeX2MRiOOI9jFJ2Inc0VZpiS5fCZOvJGfr+oCCIR 06mYl2ybCDEwbwHpRVsDLtZnEQo/Sqafq6Qsw2BeXdzwxfcjN54ev0TaSCwSeyIeywmM s9Hg== X-Gm-Message-State: AJcUukenvWLby2xzXcNzvN5LltnSmP9i0o3+kpuZN7eGLo5hLjCu2UTF wt9pIG4P7IcG7xWEHgYT+MR+iikicpI= X-Received: by 2002:a17:902:7402:: with SMTP id g2mr20212111pll.198.1547847127550; Fri, 18 Jan 2019 13:32:07 -0800 (PST) Received: from cloudburst.twiddle.net ([2001:8000:10e0:a000:c673:a6b6:fdef:1933]) by smtp.gmail.com with ESMTPSA id v12sm5833667pgg.41.2019.01.18.13.32.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 18 Jan 2019 13:32:06 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sat, 19 Jan 2019 08:30:50 +1100 Message-Id: <20190118213122.22865-17-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20190118213122.22865-1-richard.henderson@linaro.org> References: <20190118213122.22865-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::644 Subject: [Qemu-devel] [PATCH v6 17/49] linux-user: Split out brk X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: laurent@vivier.eu Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Signed-off-by: Richard Henderson --- linux-user/syscall-defs.h | 3 ++ linux-user/strace.c | 35 -------------- linux-user/syscall-mem.inc.c | 90 ++++++++++++++++++++++++++++++++++ linux-user/syscall.c | 93 ------------------------------------ linux-user/strace.list | 3 -- 5 files changed, 93 insertions(+), 131 deletions(-) -- 2.17.2 diff --git a/linux-user/syscall-defs.h b/linux-user/syscall-defs.h index 88aa1a6bfd..c3ed22ff16 100644 --- a/linux-user/syscall-defs.h +++ b/linux-user/syscall-defs.h @@ -16,6 +16,9 @@ * along with this program; if not, see . */ +SYSCALL_DEF_FULL(brk, .impl = impl_brk, + .print_ret = print_syscall_ptr_ret, + .arg_type = { ARG_PTR }); SYSCALL_DEF(close, ARG_DEC); SYSCALL_DEF(exit, ARG_DEC); #ifdef TARGET_NR_ipc diff --git a/linux-user/strace.c b/linux-user/strace.c index 5619defec8..ba541432eb 100644 --- a/linux-user/strace.c +++ b/linux-user/strace.c @@ -604,29 +604,6 @@ print_execve(const struct syscallname *name, * Variants for the return value output function */ -static void -print_syscall_ret_addr(const struct syscallname *name, abi_long ret) -{ - const char *errstr = NULL; - - if (ret < 0) { - errstr = target_strerror(-ret); - } - if (errstr) { - gemu_log(" = -1 errno=%d (%s)\n", (int)-ret, errstr); - } else { - gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); - } -} - -#if 0 /* currently unused */ -static void -print_syscall_ret_raw(struct syscallname *name, abi_long ret) -{ - gemu_log(" = 0x" TARGET_ABI_FMT_lx "\n", ret); -} -#endif - #ifdef TARGET_NR__newselect static void print_syscall_ret_newselect(const struct syscallname *name, abi_long ret) @@ -1168,18 +1145,6 @@ print_access(const struct syscallname *name, } #endif -#ifdef TARGET_NR_brk -static void -print_brk(const struct syscallname *name, - abi_long arg0, abi_long arg1, abi_long arg2, - abi_long arg3, abi_long arg4, abi_long arg5) -{ - print_syscall_prologue(name); - print_pointer(arg0, 1); - print_syscall_epilogue(name); -} -#endif - #ifdef TARGET_NR_chdir static void print_chdir(const struct syscallname *name, diff --git a/linux-user/syscall-mem.inc.c b/linux-user/syscall-mem.inc.c index d2ce0cb8cc..17ba8e3d97 100644 --- a/linux-user/syscall-mem.inc.c +++ b/linux-user/syscall-mem.inc.c @@ -42,6 +42,96 @@ static bitmask_transtbl const mmap_flags_tbl[] = { { 0, 0, 0, 0 } }; +static abi_ulong target_brk; +static abi_ulong target_original_brk; +static abi_ulong brk_page; + +void target_set_brk(abi_ulong new_brk) +{ + target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk); + brk_page = HOST_PAGE_ALIGN(target_brk); +} + +/* do_brk() must return target values and target errnos. */ +abi_long do_brk(abi_ulong new_brk) +{ + abi_long mapped_addr; + abi_ulong new_alloc_size; + + if (!new_brk) { + return target_brk; + } + if (new_brk < target_original_brk) { + return target_brk; + } + + /* + * If the new brk is less than the highest page reserved to the + * target heap allocation, set it and we're almost done... + */ + if (new_brk <= brk_page) { + /* + * Heap contents are initialized to zero, + * as for anonymous mapped pages. + */ + if (new_brk > target_brk) { + memset(g2h(target_brk), 0, new_brk - target_brk); + } + target_brk = new_brk; + return target_brk; + } + + /* + * We need to allocate more memory after the brk... Note that + * we don't use MAP_FIXED because that will map over the top of + * any existing mapping (like the one with the host libc or qemu + * itself); instead we treat "mapped but at wrong address" as + * a failure and unmap again. + */ + new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page); + mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, 0, 0)); + + if (mapped_addr == brk_page) { + /* + * Heap contents are initialized to zero, as for anonymous + * mapped pages. Technically the new pages are already + * initialized to zero since they *are* anonymous mapped + * pages, however we have to take care with the contents that + * come from the remaining part of the previous page: it may + * contains garbage data due to a previous heap usage (grown + * then shrunken). + */ + memset(g2h(target_brk), 0, brk_page - target_brk); + + target_brk = new_brk; + brk_page = HOST_PAGE_ALIGN(target_brk); + return target_brk; + } else if (mapped_addr != -1) { + /* + * Mapped but at wrong address, meaning there wasn't actually + * enough space for this brk. + */ + target_munmap(mapped_addr, new_alloc_size); + mapped_addr = -1; + } + +#if defined(TARGET_ALPHA) + /* + * We (partially) emulate OSF/1 on Alpha, which requires we + * return a proper errno, not an unchanged brk value. + */ + return -TARGET_ENOMEM; +#endif + /* For everything else, return the previous break. */ + return target_brk; +} + +SYSCALL_IMPL(brk) +{ + return do_brk(arg1); +} SYSCALL_IMPL(mlock) { diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 02010f9ae0..fbca989021 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -830,97 +830,6 @@ static inline int host_to_target_sock_type(int host_type) return target_type; } -static abi_ulong target_brk; -static abi_ulong target_original_brk; -static abi_ulong brk_page; - -void target_set_brk(abi_ulong new_brk) -{ - target_original_brk = target_brk = HOST_PAGE_ALIGN(new_brk); - brk_page = HOST_PAGE_ALIGN(target_brk); -} - -//#define DEBUGF_BRK(message, args...) do { fprintf(stderr, (message), ## args); } while (0) -#define DEBUGF_BRK(message, args...) - -/* do_brk() must return target values and target errnos. */ -abi_long do_brk(abi_ulong new_brk) -{ - abi_long mapped_addr; - abi_ulong new_alloc_size; - - DEBUGF_BRK("do_brk(" TARGET_ABI_FMT_lx ") -> ", new_brk); - - if (!new_brk) { - DEBUGF_BRK(TARGET_ABI_FMT_lx " (!new_brk)\n", target_brk); - return target_brk; - } - if (new_brk < target_original_brk) { - DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk < target_original_brk)\n", - target_brk); - return target_brk; - } - - /* If the new brk is less than the highest page reserved to the - * target heap allocation, set it and we're almost done... */ - if (new_brk <= brk_page) { - /* Heap contents are initialized to zero, as for anonymous - * mapped pages. */ - if (new_brk > target_brk) { - memset(g2h(target_brk), 0, new_brk - target_brk); - } - target_brk = new_brk; - DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <= brk_page)\n", target_brk); - return target_brk; - } - - /* We need to allocate more memory after the brk... Note that - * we don't use MAP_FIXED because that will map over the top of - * any existing mapping (like the one with the host libc or qemu - * itself); instead we treat "mapped but at wrong address" as - * a failure and unmap again. - */ - new_alloc_size = HOST_PAGE_ALIGN(new_brk - brk_page); - mapped_addr = get_errno(target_mmap(brk_page, new_alloc_size, - PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, 0, 0)); - - if (mapped_addr == brk_page) { - /* Heap contents are initialized to zero, as for anonymous - * mapped pages. Technically the new pages are already - * initialized to zero since they *are* anonymous mapped - * pages, however we have to take care with the contents that - * come from the remaining part of the previous page: it may - * contains garbage data due to a previous heap usage (grown - * then shrunken). */ - memset(g2h(target_brk), 0, brk_page - target_brk); - - target_brk = new_brk; - brk_page = HOST_PAGE_ALIGN(target_brk); - DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr == brk_page)\n", - target_brk); - return target_brk; - } else if (mapped_addr != -1) { - /* Mapped but at wrong address, meaning there wasn't actually - * enough space for this brk. - */ - target_munmap(mapped_addr, new_alloc_size); - mapped_addr = -1; - DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr != -1)\n", target_brk); - } - else { - DEBUGF_BRK(TARGET_ABI_FMT_lx " (otherwise)\n", target_brk); - } - -#if defined(TARGET_ALPHA) - /* We (partially) emulate OSF/1 on Alpha, which requires we - return a proper errno, not an unchanged brk value. */ - return -TARGET_ENOMEM; -#endif - /* For everything else, return the previous break. */ - return target_brk; -} - static inline abi_long copy_from_user_fdset(fd_set *fds, abi_ulong target_fds_addr, int n) @@ -5626,8 +5535,6 @@ static abi_long do_syscall1(void *cpu_env, int num, abi_long arg1, void *p; switch(num) { - case TARGET_NR_brk: - return do_brk(arg1); #ifdef TARGET_NR_fork case TARGET_NR_fork: return get_errno(do_fork(cpu_env, TARGET_SIGCHLD, 0, 0, 0, 0)); diff --git a/linux-user/strace.list b/linux-user/strace.list index d0160f841f..bdc1401b01 100644 --- a/linux-user/strace.list +++ b/linux-user/strace.list @@ -49,9 +49,6 @@ #ifdef TARGET_NR_break { TARGET_NR_break, "break" , NULL, NULL, NULL }, #endif -#ifdef TARGET_NR_brk -{ TARGET_NR_brk, "brk" , NULL, print_brk, print_syscall_ret_addr }, -#endif #ifdef TARGET_NR_cachectl { TARGET_NR_cachectl, "cachectl" , NULL, NULL, NULL }, #endif