From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id qPDHCPohe2CaOgAAWB0awg (envelope-from ) for ; Sat, 17 Apr 2021 13:59:22 -0400 Received: by simark.ca (Postfix, from userid 112) id 1FC251F106; Sat, 17 Apr 2021 13:59:22 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id E4D9A1EE0E for ; Sat, 17 Apr 2021 13:59:19 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A1B2C389850F; Sat, 17 Apr 2021 17:59:19 +0000 (GMT) Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by sourceware.org (Postfix) with ESMTPS id A4D50389851D for ; Sat, 17 Apr 2021 17:59:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org A4D50389851D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=sifive.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=jimw@sifive.com Received: by mail-pl1-x632.google.com with SMTP id o16so2003065plg.5 for ; Sat, 17 Apr 2021 10:59:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZfG1ataHNLSgnHkpRNMqarlfX8xXT3NPfZ4qgQUnnwg=; b=W5jPMve6/CgUYBWuK0Go0UoLrE3M2YazE6e+b+ZDnfviVqkKi8cPfBdbBgZHEaMAVT 2GmGpPsHmc1Luo8MbLdJBJqVRXOmI+uP8YnhfzOPaQUuTifsDAyW26ocTeoR005zmzcX U9kagLx0u3dODX1xZV8twfgNWpLEXs7qAcIvtugw8e26e8swvJ3h51/P/H8gUQ80DCr2 0muM1iy2MIQ1D2MycJGQtUBGGSgEZ5uJNT2ijFrcM4PZLcsy2wC6cDFsOfa4nk0k3uBH rBsdpauZyLNixHl+RUYhTaQZlae3Q8nOF3j6MJjY0jGHDgCdMVJB1N/B9Z2ioyZUyPUi CHig== 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=ZfG1ataHNLSgnHkpRNMqarlfX8xXT3NPfZ4qgQUnnwg=; b=akMFmI4s+8X1/2z7hi8Prwgmlb7MpI9sf4OQVO43+Ip/2dmtRwn9DfosyJk1FCK23h ZJ5CaKxBUpd5viJpQjpOfpOhSs1PHv5tLn9JA3FUqYVAvX490/EFRoTJx1RvnJCzGYc0 5wGbPmq1Q7iHzz0RUL3G9YG+CJ1zSNO1UQZczreGnuzJIVKThxQOTagQdKiCwjufYVqE ATa0I+eBY75qGpy8O52KDYhRWCIB/JyAf0wB4VIaOPNZ74GJL0N4pkdBI6QQn0mdIPIS DDf9itl/RSGSwp4o9Frfs6NItI30p9654/6OB4sXk9OSjhYxI3PxUr1VB2Zm5xyqZ5IA RysQ== X-Gm-Message-State: AOAM533rh65359uxt1ynlvwF4sfyOVAWWym/fELrZ5/pNLlmb5uRxmUF AINaL7DP0ei9aSu26G7guSNoP3okHTnkog== X-Google-Smtp-Source: ABdhPJz5eRFp/15qgZY5dWx72zsKhPg79/TzuknC3z7S9IFZJCa037ZD5FW3T5jN95qatWioxtXtFA== X-Received: by 2002:a17:90a:5284:: with SMTP id w4mr15684541pjh.29.1618682347485; Sat, 17 Apr 2021 10:59:07 -0700 (PDT) Received: from rohan.hsd1.ca.comcast.net ([2601:646:c180:b150:1820:3ed4:975:3fbe]) by smtp.gmail.com with ESMTPSA id u4sm5030705pfk.56.2021.04.17.10.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Apr 2021 10:59:07 -0700 (PDT) From: Jim Wilson To: gdb-patches@sourceware.org Subject: [PATCH 08/24] RISC-V sim: Add brk syscall. Date: Sat, 17 Apr 2021 10:58:15 -0700 Message-Id: <20210417175831.16413-9-jimw@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210417175831.16413-1-jimw@sifive.com> References: <20210417175831.16413-1-jimw@sifive.com> X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kuan-Lin Chen Errors-To: gdb-patches-bounces@sourceware.org Sender: "Gdb-patches" From: Kuan-Lin Chen Add endbrk field to cpu struct. Set it to highest section end address. Use it for the brk syscall support. sim/riscv/ * interp.c: Include libiberty.h, bfd.h, and elf-bfd.h. (sim_create_inferior): New locals phdr, i, phnum. Use to calculate cpu->endbrk. * sim-main.c (execute_i): In case MATCH_ECALL, handle TARGET_SYS_brk. * sim-main.h (struct _sim_cpu): Add endbrk field. --- sim/riscv/interp.c | 15 +++++++++++++++ sim/riscv/sim-main.c | 9 +++++++++ sim/riscv/sim-main.h | 1 + 3 files changed, 25 insertions(+) diff --git a/sim/riscv/interp.c b/sim/riscv/interp.c index 1bf60a4..c98f6ab 100644 --- a/sim/riscv/interp.c +++ b/sim/riscv/interp.c @@ -20,6 +20,10 @@ #include "config.h" +#include "libiberty.h" +#include "bfd.h" +#include "elf-bfd.h" + #include "sim-main.h" #include "sim-options.h" @@ -129,6 +133,8 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, { SIM_CPU *cpu = STATE_CPU (sd, 0); SIM_ADDR addr; + Elf_Internal_Phdr *phdr; + int i, phnum; /* Set the PC. */ if (abfd != NULL) @@ -137,6 +143,15 @@ sim_create_inferior (SIM_DESC sd, struct bfd *abfd, addr = 0; sim_pc_set (cpu, addr); + /* Set endbrk to highest section end address. */ + phdr = elf_tdata (abfd)->phdr; + phnum = elf_elfheader (abfd)->e_phnum; + for (i = 0; i < phnum; i++) + { + if (phdr[i].p_paddr + phdr[i].p_memsz > cpu->endbrk) + cpu->endbrk = phdr[i].p_paddr + phdr[i].p_memsz; + } + /* Standalone mode (i.e. `run`) will take care of the argv for us in sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim' with `gdb`), we need to handle it because the user can change the diff --git a/sim/riscv/sim-main.c b/sim/riscv/sim-main.c index 03ba79b..597e9c3 100644 --- a/sim/riscv/sim-main.c +++ b/sim/riscv/sim-main.c @@ -1274,6 +1274,15 @@ execute_i (SIM_CPU *cpu, unsigned_word iw, const struct riscv_opcode *op) cpu->a0 = link (oldpath, newpath); break; } + case TARGET_SYS_brk: + { + /* FIXME: Check the invalid access. */ + if (cpu->a0 == 0) + cpu->a0 = cpu->endbrk; + else + cpu->endbrk = cpu->a0; + break; + } default: cpu->a0 = -1; break; diff --git a/sim/riscv/sim-main.h b/sim/riscv/sim-main.h index e53794f..9206b94 100644 --- a/sim/riscv/sim-main.h +++ b/sim/riscv/sim-main.h @@ -63,6 +63,7 @@ struct _sim_cpu { }; }; sim_cia pc; + sim_cia endbrk; struct { #define DECLARE_CSR(name, ...) unsigned_word name; -- 2.7.4