From patchwork Tue May 19 02:53:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 186909 Delivered-To: patch@linaro.org Received: by 2002:a92:5b0a:0:0:0:0:0 with SMTP id p10csp3447083ilb; Mon, 18 May 2020 19:56:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXX8cEFHKezunwcIeNyOiiYVKv6k1Tui/OpxeFdDmtam/fSkWE6LtOUt9DJ5s6yiKee1vG X-Received: by 2002:ac8:5381:: with SMTP id x1mr13961893qtp.388.1589856960044; Mon, 18 May 2020 19:56:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589856960; cv=none; d=google.com; s=arc-20160816; b=jm9YsJxbTvtWP9LCw2Xr0xOAd0MjYX9NkMz4iUgb2ZIeOz/6JlcIgzk89seitcs4TK z4iHO3fB0iMFkhW1kKfo4j0iPSMzGy3Q0lX9twAw/O/MvuErL44FetFStO54jQEsz46i oqcSOf9sVtdsBO4MAvbMH2X6sA5yhB6D3zl+N9Wif21U80VsMdUJ8NHrEdEDsTx9X81a n/0tMoscCigMlYnAN02mwGftf05wwnoHb2c3jaRcFZBniqqTwGkmIQ+6N99KGqYwcquY D8beoGj7FvKbFsYxnUaNJXjID0fmrb9rpO5DZ4qg1ojBT0McoUaF/xF22BYl4IPdkWrb CvKg== 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:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=i6cN+V0oyKOXcFzwj12P4di1SZQuQTGDgSJO64rVCwQ=; b=yzBenqGYtDyugEN9nTEFTFwPpNdVjwhAC56rwsVvqdIC3zD/LjfCCEj9XAnWI3yPEL e4XCjnPXeTeFrHIFrPVLSTSZ1DXwuP5nUIE7m5WcC4osqxSr+v+dZQKCeUtzNXNMx1VT 4JSxu9h6QRrIK2HX4a7onpQTG8lwvx11n2Hdp6BTW26XNY50MYE1BH5zf6VQ8QtOisSE VufSjbyi71zgs9JOfgVhvQvwrjbpo/ouDtsxSs9NIU+T5L0Uii4RIt/lHalpCVy6thXL k0pLTT3tBYf0GQAvhx7bBMszWrn9dFvg2tywyNPhNZjg0XIZgXsbJ+Ma9jERJBgvt1FU gp0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ROiaR31o; 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=pass (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 5si7169374qtp.325.2020.05.18.19.55.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 18 May 2020 19:56:00 -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=pass header.i=@linaro.org header.s=google header.b=ROiaR31o; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:35504 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jasQB-0000UJ-Em for patch@linaro.org; Mon, 18 May 2020 22:55:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46854) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jasOU-0005Pb-Im for qemu-devel@nongnu.org; Mon, 18 May 2020 22:54:14 -0400 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]:46370) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jasOS-0002Qa-Cx for qemu-devel@nongnu.org; Mon, 18 May 2020 22:54:14 -0400 Received: by mail-pg1-x532.google.com with SMTP id p21so5721508pgm.13 for ; Mon, 18 May 2020 19:54:10 -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 :mime-version:content-transfer-encoding; bh=i6cN+V0oyKOXcFzwj12P4di1SZQuQTGDgSJO64rVCwQ=; b=ROiaR31orw2RW3U4tEeg/VJDeewODccnoD9B1iUPsw4wXgaO2J0YF5aIXil3ZTkYuX 3QA/vVU13JsLYPA9n+B5tVDPxB4cbXTqnEdh+qCbL2AmhIu1qZ6ooB60ACnJlFGQTzQE b3vFuyPX0JHu41y9ADcldb5YlytGwZpT3pz82lE4U+AYCb64u5Sb/VHplzipZnSezWed w1E8sxa12bg0Fg9Y4KIi1DPPIRLB+hGt6Ct0axhk8glOL4V/JB5HmO6BryoNTcAd/+rS lHVebDPnGZ2iNIKwO8G0ztjldVu7AnDNZCujS8eKpM5liJlgMsQJ3r0eDW8ecy4ntNUV P1NA== 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:mime-version:content-transfer-encoding; bh=i6cN+V0oyKOXcFzwj12P4di1SZQuQTGDgSJO64rVCwQ=; b=jfOFLCo4f2VW4NYGXGZ0dx3uuly8QkLklF8YYu8+nO7ULP99U21Ug22ueb6v7Ki/Vo CMglNru9oocjOiGRnmGYghhxczSLJcAC8l7gchK+rdATqXbI0FafWukplgQJ3sJJNb0r 1CTa2CnAFB6OAdpow/qyL4uVGweqfVP/WhiY2u51EjQp1XvDisKWU4eTlx0ZGgHdEij8 1dCRkdjtraFBV4xCT35fz0Lnv0CFGZPIhJFDQZjICXLKVuNXyuQTgnKl74OnApDG6B97 Ax9yoMgMSrgUpqlWhpz/2dZ7j2kADZnbvcobDW9JvqBY2DyAwAZ9NXNO5nM8B3m5sEUf Q54w== X-Gm-Message-State: AOAM533GXq6pqhAUqLFRG8X7UN8z236lMox1slHnOSuQC2BAaKoY34vS a5Wim8IcQOKbVHmis4J+seOcJfDuPDc= X-Received: by 2002:a62:fc52:: with SMTP id e79mr20575485pfh.297.1589856849122; Mon, 18 May 2020 19:54:09 -0700 (PDT) Received: from localhost.localdomain (174-21-143-238.tukw.qwest.net. [174.21.143.238]) by smtp.gmail.com with ESMTPSA id a2sm8772418pgh.57.2020.05.18.19.54.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 19:54:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [RISU v2 09/17] Unify i/o functions and use RisuResult Date: Mon, 18 May 2020 19:53:47 -0700 Message-Id: <20200519025355.4420-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200519025355.4420-1-richard.henderson@linaro.org> References: <20200519025355.4420-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x532.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, alex.bennee@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Push the trace check down from the function calling the reginfo function down into the i/o function. This means we don't have to pass a function pointer. Return a RisuResult from the i/o functions. This fixes a minor bug in send_register_info (even before the conversion to RisuResult), which returned the write_fn result directly. Signed-off-by: Richard Henderson --- risu.h | 25 ++++++++--------- comms.c | 8 +++--- reginfo.c | 60 ++++++++++++++++++++-------------------- risu.c | 82 ++++++++++++++++++++++--------------------------------- 4 files changed, 80 insertions(+), 95 deletions(-) -- 2.20.1 Reviewed-by: Alex Bennée diff --git a/risu.h b/risu.h index e6d07eb..f44b781 100644 --- a/risu.h +++ b/risu.h @@ -34,13 +34,6 @@ void process_arch_opt(int opt, const char *arg); #include REGINFO_HEADER(ARCH) -/* Socket related routines */ -int master_connect(int port); -int apprentice_connect(const char *hostname, int port); -int send_data_pkt(int sock, void *pkt, int pktlen); -int recv_data_pkt(int sock, void *pkt, int pktlen); -void send_response_byte(int sock, int resp); - extern uintptr_t image_start_address; extern void *memblock; @@ -80,6 +73,13 @@ typedef struct { uint32_t risu_op; } trace_header_t; +/* Socket related routines */ +int master_connect(int port); +int apprentice_connect(const char *hostname, int port); +RisuResult send_data_pkt(int sock, void *pkt, int pktlen); +RisuResult recv_data_pkt(int sock, void *pkt, int pktlen); +void send_response_byte(int sock, int resp); + /* Functions operating on reginfo */ /* Function prototypes for read/write helper functions. @@ -88,23 +88,22 @@ typedef struct { * recv_and_compare_register_info which can either be backed by the * traditional network socket or a trace file. */ -typedef int (*write_fn) (void *ptr, size_t bytes); -typedef int (*read_fn) (void *ptr, size_t bytes); -typedef void (*respond_fn) (RisuResult response); +RisuResult write_buffer(void *ptr, size_t bytes); +RisuResult read_buffer(void *ptr, size_t bytes); +void respond(RisuResult response); /* * Send the register information from the struct ucontext down the socket. * NB: called from a signal handler. */ -RisuResult send_register_info(write_fn write_fn, void *uc); +RisuResult send_register_info(void *uc); /* * Read register info from the socket and compare it with that from the * ucontext. * NB: called from a signal handler. */ -RisuResult recv_and_compare_register_info(read_fn read_fn, - respond_fn respond, void *uc); +RisuResult recv_and_compare_register_info(void *uc); /* Print a useful report on the status of the last comparison * done in recv_and_compare_register_info(). This is called on diff --git a/comms.c b/comms.c index 861e845..21968da 100644 --- a/comms.c +++ b/comms.c @@ -168,7 +168,7 @@ ssize_t safe_writev(int fd, struct iovec *iov_in, int iovcnt) * Note that both ends must agree on the length of the * block of data. */ -int send_data_pkt(int sock, void *pkt, int pktlen) +RisuResult send_data_pkt(int sock, void *pkt, int pktlen) { unsigned char resp; /* First we send the packet length as a network-order 32 bit value. @@ -196,7 +196,7 @@ int send_data_pkt(int sock, void *pkt, int pktlen) return resp; } -int recv_data_pkt(int sock, void *pkt, int pktlen) +RisuResult recv_data_pkt(int sock, void *pkt, int pktlen) { uint32_t net_pktlen; recv_bytes(sock, &net_pktlen, sizeof(net_pktlen)); @@ -206,10 +206,10 @@ int recv_data_pkt(int sock, void *pkt, int pktlen) * a response back. */ recv_and_discard_bytes(sock, net_pktlen); - return 1; + return RES_BAD_IO; } recv_bytes(sock, pkt, pktlen); - return 0; + return RES_OK; } void send_response_byte(int sock, int resp) diff --git a/reginfo.c b/reginfo.c index b909a1f..fee025e 100644 --- a/reginfo.c +++ b/reginfo.c @@ -21,10 +21,11 @@ uint8_t apprentice_memblock[MEMBLOCKLEN]; static int mem_used; static int packet_mismatch; -RisuResult send_register_info(write_fn write_fn, void *uc) +RisuResult send_register_info(void *uc) { struct reginfo ri; trace_header_t header; + RisuResult res; RisuOp op; reginfo_init(&ri, uc); @@ -33,8 +34,9 @@ RisuResult send_register_info(write_fn write_fn, void *uc) /* Write a header with PC/op to keep in sync */ header.pc = get_pc(&ri); header.risu_op = op; - if (write_fn(&header, sizeof(header)) != 0) { - return RES_BAD_IO; + res = write_buffer(&header, sizeof(header)); + if (res != RES_OK) { + return res; } switch (op) { @@ -45,11 +47,12 @@ RisuResult send_register_info(write_fn write_fn, void *uc) * Do a simple register compare on (a) explicit request * (b) end of test (c) a non-risuop UNDEF */ - if (write_fn(&ri, reginfo_size()) != 0) { - return RES_BAD_IO; + res = write_buffer(&ri, reginfo_size()); + /* For OP_TEST_END, force exit. */ + if (res == RES_OK && op == OP_TESTEND) { + res = RES_END; } - /* For OP_TEST_END, force return 1 to exit. */ - return op == OP_TESTEND ? RES_END : RES_OK; + break; case OP_SETMEMBLOCK: memblock = (void *)(uintptr_t)get_reginfo_paramreg(&ri); break; @@ -58,12 +61,11 @@ RisuResult send_register_info(write_fn write_fn, void *uc) get_reginfo_paramreg(&ri) + (uintptr_t)memblock); break; case OP_COMPAREMEM: - return write_fn(memblock, MEMBLOCKLEN); - break; + return write_buffer(memblock, MEMBLOCKLEN); default: abort(); } - return RES_OK; + return res; } /* Read register info from the socket and compare it with that from the @@ -74,29 +76,29 @@ RisuResult send_register_info(write_fn write_fn, void *uc) * that says whether it is register or memory data, so if the two * sides get out of sync then we will fail obscurely. */ -RisuResult recv_and_compare_register_info(read_fn read_fn, - respond_fn resp_fn, void *uc) +RisuResult recv_and_compare_register_info(void *uc) { - RisuResult resp = RES_OK; + RisuResult res; trace_header_t header; RisuOp op; reginfo_init(&master_ri, uc); op = get_risuop(&master_ri); - if (read_fn(&header, sizeof(header)) != 0) { - return RES_BAD_IO; + res = read_buffer(&header, sizeof(header)); + if (res != RES_OK) { + return res; } if (header.risu_op != op) { /* We are out of sync */ - resp = RES_BAD_IO; - resp_fn(resp); - return resp; + res = RES_BAD_IO; + respond(res); + return res; } /* send OK for the header */ - resp_fn(RES_OK); + respond(RES_OK); switch (op) { case OP_COMPARE: @@ -105,16 +107,16 @@ RisuResult recv_and_compare_register_info(read_fn read_fn, /* Do a simple register compare on (a) explicit request * (b) end of test (c) a non-risuop UNDEF */ - if (read_fn(&apprentice_ri, reginfo_size())) { + res = read_buffer(&apprentice_ri, reginfo_size()); + if (res != RES_OK) { packet_mismatch = 1; - resp = RES_BAD_IO; } else if (!reginfo_is_eq(&master_ri, &apprentice_ri)) { /* register mismatch */ - resp = RES_MISMATCH; + res = RES_MISMATCH; } else if (op == OP_TESTEND) { - resp = RES_END; + res = RES_END; } - resp_fn(resp); + respond(res); break; case OP_SETMEMBLOCK: memblock = (void *)(uintptr_t)get_reginfo_paramreg(&master_ri); @@ -125,20 +127,20 @@ RisuResult recv_and_compare_register_info(read_fn read_fn, break; case OP_COMPAREMEM: mem_used = 1; - if (read_fn(apprentice_memblock, MEMBLOCKLEN)) { + res = read_buffer(apprentice_memblock, MEMBLOCKLEN); + if (res != RES_OK) { packet_mismatch = 1; - resp = RES_BAD_IO; } else if (memcmp(memblock, apprentice_memblock, MEMBLOCKLEN) != 0) { /* memory mismatch */ - resp = RES_MISMATCH; + res = RES_MISMATCH; } - resp_fn(resp); + respond(res); break; default: abort(); } - return resp; + return res; } /* Print a useful report on the status of the last comparison diff --git a/risu.c b/risu.c index 8796975..78c6b8f 100644 --- a/risu.c +++ b/risu.c @@ -46,44 +46,15 @@ static sigjmp_buf jmpbuf; #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) -/* Master functions */ +/* I/O functions */ -int read_sock(void *ptr, size_t bytes) -{ - return recv_data_pkt(comm_fd, ptr, bytes); -} - -int write_trace(void *ptr, size_t bytes) +RisuResult read_buffer(void *ptr, size_t bytes) { size_t res; -#ifdef HAVE_ZLIB - if (comm_fd == STDOUT_FILENO) { -#endif - res = write(comm_fd, ptr, bytes); -#ifdef HAVE_ZLIB - } else { - res = gzwrite(gz_trace_file, ptr, bytes); + if (!trace) { + return recv_data_pkt(comm_fd, ptr, bytes); } -#endif - return (res == bytes) ? 0 : 1; -} - -void respond_sock(RisuResult r) -{ - send_response_byte(comm_fd, r); -} - -/* Apprentice function */ - -int write_sock(void *ptr, size_t bytes) -{ - return send_data_pkt(comm_fd, ptr, bytes); -} - -int read_trace(void *ptr, size_t bytes) -{ - size_t res; #ifdef HAVE_ZLIB if (comm_fd == STDIN_FILENO) { @@ -95,21 +66,34 @@ int read_trace(void *ptr, size_t bytes) } #endif - return (res == bytes) ? 0 : 1; + return res == bytes ? RES_OK : RES_BAD_IO; } -void respond_trace(RisuResult r) +RisuResult write_buffer(void *ptr, size_t bytes) { - switch (r) { - case RES_OK: - case RES_END: - break; - default: - /* mismatch - if tracing we need to report, otherwise barf */ - if (!trace) { - abort(); - } - break; + size_t res; + + if (!trace) { + return send_data_pkt(comm_fd, ptr, bytes); + } + +#ifdef HAVE_ZLIB + if (comm_fd == STDOUT_FILENO) { +#endif + res = write(comm_fd, ptr, bytes); +#ifdef HAVE_ZLIB + } else { + res = gzwrite(gz_trace_file, ptr, bytes); + } +#endif + + return res == bytes ? RES_OK : RES_BAD_IO; +} + +void respond(RisuResult r) +{ + if (!trace) { + send_response_byte(comm_fd, r); } } @@ -119,9 +103,9 @@ static void master_sigill(int sig, siginfo_t *si, void *uc) signal_count++; if (trace) { - r = send_register_info(write_trace, uc); + r = send_register_info(uc); } else { - r = recv_and_compare_register_info(read_sock, respond_sock, uc); + r = recv_and_compare_register_info(uc); } switch (r) { @@ -141,9 +125,9 @@ static void apprentice_sigill(int sig, siginfo_t *si, void *uc) signal_count++; if (trace) { - r = recv_and_compare_register_info(read_trace, respond_trace, uc); + r = recv_and_compare_register_info(uc); } else { - r = send_register_info(write_sock, uc); + r = send_register_info(uc); } switch (r) {