From nobody Fri Nov 7 12:59:10 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1547847443129749.5807840815329; Fri, 18 Jan 2019 13:37:23 -0800 (PST) Received: from localhost ([127.0.0.1]:47458 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gkbpD-0000Nh-Lo for importer@patchew.org; Fri, 18 Jan 2019 16:37:15 -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) 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) 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-Google-Smtp-Source: ALg8bN7mdhaDEAvmYmft3nnSU2c3/cHlHLk+rIQ6C0nwklbZgM1YSJs8KOR9CwVs62j9iKdhUuRJZA== X-Received: by 2002:a17:902:7402:: with SMTP id g2mr20212111pll.198.1547847127550; Fri, 18 Jan 2019 13:32:07 -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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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(-) 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 . */ =20 +SYSCALL_DEF_FULL(brk, .impl =3D impl_brk, + .print_ret =3D print_syscall_ptr_ret, + .arg_type =3D { 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 */ =20 -static void -print_syscall_ret_addr(const struct syscallname *name, abi_long ret) -{ - const char *errstr =3D NULL; - - if (ret < 0) { - errstr =3D target_strerror(-ret); - } - if (errstr) { - gemu_log(" =3D -1 errno=3D%d (%s)\n", (int)-ret, errstr); - } else { - gemu_log(" =3D 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(" =3D 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 =20 -#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[] =3D { { 0, 0, 0, 0 } }; =20 +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 =3D target_brk =3D HOST_PAGE_ALIGN(new_brk); + brk_page =3D 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 <=3D 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 =3D 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 =3D HOST_PAGE_ALIGN(new_brk - brk_page); + mapped_addr =3D get_errno(target_mmap(brk_page, new_alloc_size, + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, 0, 0)); + + if (mapped_addr =3D=3D 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 =3D new_brk; + brk_page =3D HOST_PAGE_ALIGN(target_brk); + return target_brk; + } else if (mapped_addr !=3D -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 =3D -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); +} =20 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_ty= pe) return target_type; } =20 -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 =3D target_brk =3D HOST_PAGE_ALIGN(new_brk); - brk_page =3D 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 <=3D 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 =3D new_brk; - DEBUGF_BRK(TARGET_ABI_FMT_lx " (new_brk <=3D 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 =3D HOST_PAGE_ALIGN(new_brk - brk_page); - mapped_addr =3D get_errno(target_mmap(brk_page, new_alloc_size, - PROT_READ|PROT_WRITE, - MAP_ANON|MAP_PRIVATE, 0, 0)); - - if (mapped_addr =3D=3D 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 =3D new_brk; - brk_page =3D HOST_PAGE_ALIGN(target_brk); - DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr =3D=3D brk_page)\n", - target_brk); - return target_brk; - } else if (mapped_addr !=3D -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 =3D -1; - DEBUGF_BRK(TARGET_ABI_FMT_lx " (mapped_addr !=3D -1)\n", target_br= k); - } - 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, a= bi_long arg1, void *p; =20 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 --=20 2.17.2