From patchwork Sun May 19 20:36:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164502 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp4784409ili; Sun, 19 May 2019 13:48:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqwfvGdB3HyqoahoO/8MNRg666+ovQNsVv2p3afEwpHRMd0sbrvKctuYAMrycPeOQU+bm/61 X-Received: by 2002:a17:906:1fc4:: with SMTP id e4mr35925455ejt.81.1558298896783; Sun, 19 May 2019 13:48:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558298896; cv=none; d=google.com; s=arc-20160816; b=wgo4ujog0Dhkb1cWAehHIvcGs2TCvGyBSFCLQwDTXNpPOKh/h2rF/m80OvLa5S3nXz HQ70SKSbFTH3jKaCtW+0kgN8woPHWMJ+vOi5z440plRemjXX6SUabkuKIgrYjk4IaHLB csd2Wqoe4CxAcOGMWDiuBPB27ALNx5edRNDzFpsG5hsS0eygYGrmZg0FHi7OCIN1lh1J uS6DfhK4jjhtDlndlA+wgF/zrmMv5UyLiSxjTYu7NHwL7nmmUQZD+Yxv4sBEC5f5ndWM Cd55ZrQwwYZ0qYv4WWIkpK5JgTakaZisbe9PDEybTt6eJO3fcryHN7jgEPxIFAx55jjT qXeA== 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=mCaCI2Aj5tC2IpfdxPu1UNH/KS+6aMoVb/BQvf6d4j4=; b=wY/n/OWy/0Qcm0TTqp/szntiqpthjwm4HOTmwOZQxf9/VmHl0ruIHEzv50vbOjNYWn MXC/wkIrRHief2S5g2cLr47y0n0qUf/MXpbvadu7wKlGf8nk64DgM8ZLxI81zFXhCpHy P2dwAijyxp2PpMQfq8DRVhuztsJl9iflpkaMUDyR6eg9c8YK4Ittv+PtUJI1Bs9jnjxs 3zfNJ78Ls63QHuA/ULd4nk0yFTOeEi4Tl5HKm5LMUeCGfLd0WB2IWe54MbvdZhh+gPXi ldKkdxx7fVKcOBFtFXPaaJFHbZh3JZ5uvn2BGkfEHSMO/ISZkCVS+GuP1gk2R52zDQy4 Gs+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Kmei6DvM; 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 g8si1461858eje.360.2019.05.19.13.48.16 for (version=TLS1 cipher=AES128-SHA bits=128/128); Sun, 19 May 2019 13:48:16 -0700 (PDT) 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=Kmei6DvM; 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]:53254 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSSj9-0003KS-JK for patch@linaro.org; Sun, 19 May 2019 16:48:15 -0400 Received: from eggs.gnu.org ([209.51.188.92]:50303) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSSZ8-000395-AH for qemu-devel@nongnu.org; Sun, 19 May 2019 16:37:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hSSZ6-0003kf-CI for qemu-devel@nongnu.org; Sun, 19 May 2019 16:37:54 -0400 Received: from mail-pl1-x642.google.com ([2607:f8b0:4864:20::642]:44234) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hSSZ6-0003jY-42 for qemu-devel@nongnu.org; Sun, 19 May 2019 16:37:52 -0400 Received: by mail-pl1-x642.google.com with SMTP id c5so5690537pll.11 for ; Sun, 19 May 2019 13:37:52 -0700 (PDT) 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=mCaCI2Aj5tC2IpfdxPu1UNH/KS+6aMoVb/BQvf6d4j4=; b=Kmei6DvMOhI+sXk4Bjs7SeJuH3jJYoD7ED4pYajTKSF/tghvhB/LFLnN1MZNmgGdpW tTGnhJvRO37FaGg4tWGT0unzkfueIqkyngEiUQkeE6vbg2TjPh3+rdujhZeluv0KzlFP kxD8Pbw3LM6yagoniOknL9d+ZfidIikCyKFk3BJxHT/EDixK/GvFcLXUgykk9q2GwSxC UVoYdMtQoDlk3dUCYvbPZKRUXfcfA3mhOBtZWbpIX6aHBgxzBEEQzH1GAgbp6digMePk GDFeqfa34hXbV8YaifR8UHw/U6vtn2Twma/dnYxXYsji9jChZ3o3JXnlpZuyhBV3dtLz Vg+Q== 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=mCaCI2Aj5tC2IpfdxPu1UNH/KS+6aMoVb/BQvf6d4j4=; b=sPrOEMa7SFglPf84Nch8QGqDyfvKmpOXG1a1PU/L3OukWYjrrhZQg/DQ0a4nWUor3m 76yHqtEiDplu2VeJeJnqiD7IhD/zDP2Ll9wBGSFmG7IZCGoW1nqVzg4sg1CJ0yryaEjx 5nXsCrIa/HlBtnbx1yIRpK/vmJSdos60hwoDXjW1jNdo0EuflyVuyZ8bxtz/jIqaceMt 1nPX8p/rmR3huxgBR7sZjkyAYlNtM/qd2TcphFxZPsXBcWurEkwOUbaZBHuN01N5G7dd UX7inOieZYXdzvaTEfzq32knUAjIE+v0EcDQng8Ear6oNiXdMUVqQLPCRj5ZhC6yateF OKew== X-Gm-Message-State: APjAAAVVUO/XJaS85ilSEn+rir/h7C7QuUo5VuY9I5/9a6dEF8HzktO4 Qoj8e0prXgC2XIXqbo3eQy+/wbSa9dA= X-Received: by 2002:a17:902:2bc9:: with SMTP id l67mr35897710plb.171.1558298270873; Sun, 19 May 2019 13:37:50 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id i7sm11397448pfo.19.2019.05.19.13.37.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 19 May 2019 13:37:50 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Sun, 19 May 2019 13:36:28 -0700 Message-Id: <20190519203726.20729-17-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190519203726.20729-1-richard.henderson@linaro.org> References: <20190519203726.20729-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::642 Subject: [Qemu-devel] [PATCH v7 16/74] 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.1 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 a767227ac1..0a2c6764db 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 c72d24aa76..4c9953a7ab 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -820,97 +820,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) @@ -5681,8 +5590,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 cc0bb10a7a..aff6d1d73d 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