From 1eb6279c5c0c1494d64989f53b580936931ba5bf Mon Sep 17 00:00:00 2001 From: drahn Date: Tue, 4 May 2021 04:17:53 +0000 Subject: [PATCH] riscv64 brk()/sbrk() implementations, based off a combination of aarch64/powerpc64 ok kettenis@ --- lib/libc/arch/riscv64/sys/brk.S | 58 ++++++++++++++++++++++++++++++++ lib/libc/arch/riscv64/sys/sbrk.S | 50 +++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 lib/libc/arch/riscv64/sys/brk.S create mode 100644 lib/libc/arch/riscv64/sys/sbrk.S diff --git a/lib/libc/arch/riscv64/sys/brk.S b/lib/libc/arch/riscv64/sys/brk.S new file mode 100644 index 00000000000..74a1c2a68bf --- /dev/null +++ b/lib/libc/arch/riscv64/sys/brk.S @@ -0,0 +1,58 @@ +/* $OpenBSD: brk.S,v 1.1 2021/05/04 04:17:53 drahn Exp $ */ +/* + * Copyright (c) 2021 Dale Rahn + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "SYS.h" + + .extern __curbrk + .extern _C_LABEL(_end) + + .data + .align 3 +__minbrk: + .dword _C_LABEL(_end) + + + .weak brk +ENTRY(brk) + RETGUARD_SETUP(brk, t6) + lla t1, _C_LABEL(__minbrk) + ld t5, 0(t1) + bgeu a0, t5, .L_brk_call + mv a0, t5 + +.L_brk_call: + mv a2, a0 + lla t1, _C_LABEL(__curbrk) + + li t0, SYS_break + ecall + + /* check for error */ + beqz t0, .L_brk_ok + + sw a0, TP_OFFSET_ERRNO(tp) + li a0, -1 + j .L_done + + /* update, __curbrk and return */ +.L_brk_ok: + sd a2, 0(t1) + li a0, 0 /* # return 0 */ +.L_done: + RETGUARD_CHECK(brk, t6); + ret +END(brk) diff --git a/lib/libc/arch/riscv64/sys/sbrk.S b/lib/libc/arch/riscv64/sys/sbrk.S new file mode 100644 index 00000000000..02d02d3900d --- /dev/null +++ b/lib/libc/arch/riscv64/sys/sbrk.S @@ -0,0 +1,50 @@ +/* $OpenBSD: sbrk.S,v 1.1 2021/05/04 04:17:53 drahn Exp $ */ +/* + * Copyright (c) 2021 Dale Rahn + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "SYS.h" + + .extern _C_LABEL(_end) + + .data + .align 3 + .globl __curbrk + .hidden __curbrk + .type __curbrk,@object +__curbrk: + .dword _C_LABEL(_end) + END(__curbrk) + + .weak sbrk +ENTRY(sbrk) + RETGUARD_SETUP(sbrk, t6) + + lla t1, _C_LABEL(__curbrk) + ld a2, 0(t1) + add a0, a0, a2 // compute current break + mv t3, a0 + SYSTRAP(break) + beqz t0, .L_sbrk_ok + sw a0, TP_OFFSET_ERRNO(tp) + li a0, -1 + j .L_sbrk_fail +.L_sbrk_ok: + sd t3, 0(t1) // save current val of curbrk from above + mv a0, a2 // return old val of curbrk from above +.L_sbrk_fail: + RETGUARD_CHECK(sbrk, t6); + ret +END(sbrk) -- 2.20.1