From 57b429e886b2d4e69b2a350ac82e117ab72fcef4 Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Mon, 13 Feb 2023 18:59:34 +0000 Subject: [PATCH 1/4] Adding mpslibfs.c dummy plinth to check whether core MPS is freestanding. --- code/mpslibfs.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 code/mpslibfs.c diff --git a/code/mpslibfs.c b/code/mpslibfs.c new file mode 100644 index 0000000000..a5066f5ed4 --- /dev/null +++ b/code/mpslibfs.c @@ -0,0 +1,158 @@ +/* mpslibfs.c: RAVENBROOK MEMORY POOL SYSTEM LIBRARY INTERFACE (FREESTANDING) + * + * $Id$ + * Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license. + * Portions copyright (c) 2002 Global Graphics Software. + * + * .purpose: The purpose of this code is to test at compile time + * whether the code MPS can be compiled as freestanding [FIXME: ref?] + * + * .readership: For MPS client application developers and MPS developers. + * .sources: + */ + +#include "mpslib.h" +#include "mpsio.h" + +#include "mpstd.h" +#include "event.h" + +int mps_lib_get_EOF(void) +{ + NOTREACHED; +} + +mps_lib_FILE *mps_lib_get_stderr(void) +{ + NOTREACHED; +} + +mps_lib_FILE *mps_lib_get_stdout(void) +{ + NOTREACHED; +} + +int mps_lib_fputc(int c, mps_lib_FILE *stream) +{ + NOTREACHED; +} + +int mps_lib_fputs(const char *s, mps_lib_FILE *stream) +{ + NOTREACHED; +} + +void mps_lib_assert_fail(const char *file, + unsigned line, + const char *condition) +{ + for (;;) + NOOP; +} + +mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t handler) +{ + NOTREACHED; +} + + +void *(mps_lib_memset)(void *s, int c, size_t n) +{ + NOTREACHED; +} + +void *(mps_lib_memcpy)(void *s1, const void *s2, size_t n) +{ + NOTREACHED; +} + +int (mps_lib_memcmp)(const void *s1, const void *s2, size_t n) +{ + NOTREACHED; +} + +mps_clock_t mps_clock(void) +{ + NOTREACHED; +} + + +mps_clock_t mps_clocks_per_sec(void) +{ + NOTREACHED; +} + +unsigned long mps_lib_telemetry_control(void) +{ + NOTREACHED; +} + + +mps_res_t mps_io_create(mps_io_t *mps_io_r) +{ + NOTREACHED; +} + + +void mps_io_destroy(mps_io_t mps_io) +{ + NOTREACHED; +} + + +mps_res_t mps_io_write(mps_io_t mps_io, void *buf, size_t size) +{ + NOTREACHED; +} + + +mps_res_t mps_io_flush(mps_io_t mps_io) +{ + NOTREACHED; +} + + +/* main -- dummy entry point + * + * Main is included here so that a command like:: + * + * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI mps.c mpslibfs.c + * + * can run to completion of the linker and reveal external + * dependencies. + */ + +int main(void) +{ + return 0; +} + + +/* C. COPYRIGHT AND LICENSE + * + * Copyright (C) 2001-2023 Ravenbrook Limited . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ From 9bc1f9851a559f7d917cb1f85a621a9c77aaf491 Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Mon, 13 Feb 2023 22:18:40 +0000 Subject: [PATCH 2/4] Adding freestanding stubs for MPS library and VM so that C library can be excluded. --- code/mps.c | 3 +- code/mpslibfs.c | 20 +++-------- code/vmfs.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 code/vmfs.c diff --git a/code/mps.c b/code/mps.c index 41c8d076af..97599737e5 100644 --- a/code/mps.c +++ b/code/mps.c @@ -96,6 +96,8 @@ #if defined(PLINTH) /* see CONFIG_PLINTH_NONE in config.h */ #include "mpsliban.c" #include "mpsioan.c" +/* FIXME: Is vman part of the plinth? Not really. Explain this. */ +#include "vman.c" /* malloc-based pseudo memory mapping */ #endif /* Generic ("ANSI") platform */ @@ -104,7 +106,6 @@ #include "lockan.c" /* generic locks */ #include "than.c" /* generic threads manager */ -#include "vman.c" /* malloc-based pseudo memory mapping */ #include "protan.c" /* generic memory protection */ #include "prmcan.c" /* generic operating system mutator context */ #include "prmcanan.c" /* generic architecture mutator context */ diff --git a/code/mpslibfs.c b/code/mpslibfs.c index a5066f5ed4..c5a3acfed1 100644 --- a/code/mpslibfs.c +++ b/code/mpslibfs.c @@ -6,6 +6,10 @@ * * .purpose: The purpose of this code is to test at compile time * whether the code MPS can be compiled as freestanding [FIXME: ref?] + * with a command like:: + * + * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI \ + * --entry mps_lib_assert_fail mps.c mpslibfs.c vmfs.c * * .readership: For MPS client application developers and MPS developers. * .sources: @@ -112,22 +116,6 @@ mps_res_t mps_io_flush(mps_io_t mps_io) } -/* main -- dummy entry point - * - * Main is included here so that a command like:: - * - * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI mps.c mpslibfs.c - * - * can run to completion of the linker and reveal external - * dependencies. - */ - -int main(void) -{ - return 0; -} - - /* C. COPYRIGHT AND LICENSE * * Copyright (C) 2001-2023 Ravenbrook Limited . diff --git a/code/vmfs.c b/code/vmfs.c new file mode 100644 index 0000000000..c9f658c4cb --- /dev/null +++ b/code/vmfs.c @@ -0,0 +1,93 @@ +/* vmfs.c: FREESTANDING VM: VIRTUAL MEMORY MAPPING STUB + * + * $Id$ + * Copyright (c) 2001-2023 Ravenbrook Limited. See end of file for license. + * + * .purpose: The purpose of this code is to test at compile time + * whether the code MPS can be compiled as freestanding [FIXME: ref?] + * with a command like:: + * + * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI \ + * --entry mps_lib_assert_fail mps.c mpslibfs.c vmfs.c + * + * .sources: design.mps.lib + */ + +#include "mpm.h" +#include "vm.h" + +SRCID(vmfs, "$Id$"); + + +Size PageSize(void) +{ + NOTREACHED; +} + + +Res VMParamFromArgs(void *params, size_t paramSize, ArgList args) +{ + NOTREACHED; +} + + +/* VMInit -- reserve some virtual address space, and create a VM structure */ + +Res VMInit(VM vm, Size size, Size grainSize, void *params) +{ + NOTREACHED; +} + + +/* VMFinish -- release all address space and finish VM structure */ + +void VMFinish(VM vm) +{ + NOTREACHED; +} + + +/* VMMap -- map the given range of memory */ + +Res VMMap(VM vm, Addr base, Addr limit) +{ + NOTREACHED; +} + + +/* VMUnmap -- unmap the given range of memory */ + +void VMUnmap(VM vm, Addr base, Addr limit) +{ + NOTREACHED; +} + + +/* C. COPYRIGHT AND LICENSE + * + * Copyright (C) 2001-2023 Ravenbrook Limited . + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ From ab31a8bfa89efe64ce7a7ab62da36788fc7002aa Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Tue, 14 Feb 2023 08:01:23 +0000 Subject: [PATCH 3/4] Eliminating implicit non-freestanding dependency on memcpy from structure assignment . --- code/root.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/root.c b/code/root.c index 42336bbe75..606bd5c3d0 100644 --- a/code/root.c +++ b/code/root.c @@ -201,7 +201,7 @@ static Res rootCreate(Root *rootReturn, Arena arena, root->arena = arena; root->rank = rank; root->var = type; - root->the = *theUnionP; + mps_lib_memcpy(&root->the, theUnionP, sizeof(root->the)); root->grey = TraceSetEMPTY; root->summary = RefSetUNIV; root->mode = mode; From d17476f49b74e3220bb83b28684661656391126f Mon Sep 17 00:00:00 2001 From: Richard Brooksby Date: Tue, 14 Feb 2023 08:28:14 +0000 Subject: [PATCH 4/4] Lifting vman.c dependencies over the plinth so that vman.c can be compiled when checking for freestanding. --- code/mps.c | 3 +- code/mpslib.h | 7 ++++ code/mpsliban.c | 10 ++++++ code/mpslibfs.c | 18 +++++++--- code/vman.c | 7 ++-- code/vmfs.c | 93 ------------------------------------------------- 6 files changed, 35 insertions(+), 103 deletions(-) delete mode 100644 code/vmfs.c diff --git a/code/mps.c b/code/mps.c index 97599737e5..c1719e2112 100644 --- a/code/mps.c +++ b/code/mps.c @@ -96,8 +96,6 @@ #if defined(PLINTH) /* see CONFIG_PLINTH_NONE in config.h */ #include "mpsliban.c" #include "mpsioan.c" -/* FIXME: Is vman part of the plinth? Not really. Explain this. */ -#include "vman.c" /* malloc-based pseudo memory mapping */ #endif /* Generic ("ANSI") platform */ @@ -110,6 +108,7 @@ #include "prmcan.c" /* generic operating system mutator context */ #include "prmcanan.c" /* generic architecture mutator context */ #include "span.c" /* generic stack probe */ +#include "vman.c" /* malloc-based pseudo memory mapping */ /* macOS on ARM64 built with Clang */ diff --git a/code/mpslib.h b/code/mpslib.h index f7f1eac6fc..70ca75cc62 100644 --- a/code/mpslib.h +++ b/code/mpslib.h @@ -55,6 +55,13 @@ typedef void (*mps_lib_assert_fail_t)(const char *, unsigned, const char *); extern mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t); +/* Allocate or free memory. Analogous to `malloc` and `free` from + stdlib.h. NOTE: These must not be used outside the ANSI VM + (vman.c) to ensure the MPS remains able to bootstrap itself from + raw memory. */ +extern void *mps_lib_malloc(size_t); +extern void mps_lib_free(void *); + /* Set, copy, or compare memory. Analagous to `memset`, `memcpy`, and `memcmp` from string.h. */ extern void *(mps_lib_memset)(void *, int, size_t); diff --git a/code/mpsliban.c b/code/mpsliban.c index a7927c80cd..b0b1887545 100644 --- a/code/mpsliban.c +++ b/code/mpsliban.c @@ -95,6 +95,16 @@ mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t handler) } +void *mps_lib_malloc(size_t size) +{ + return malloc(size); +} + +void mps_lib_free(void *p) +{ + free(p); +} + void *(mps_lib_memset)(void *s, int c, size_t n) { return memset(s, c, n); diff --git a/code/mpslibfs.c b/code/mpslibfs.c index c5a3acfed1..354213d3f2 100644 --- a/code/mpslibfs.c +++ b/code/mpslibfs.c @@ -1,15 +1,15 @@ -/* mpslibfs.c: RAVENBROOK MEMORY POOL SYSTEM LIBRARY INTERFACE (FREESTANDING) +/* mpslibfs.c: FREESTANDING LIBRARY INTERFACE STUB * * $Id$ * Copyright (c) 2001-2020 Ravenbrook Limited. See end of file for license. * Portions copyright (c) 2002 Global Graphics Software. * * .purpose: The purpose of this code is to test at compile time - * whether the code MPS can be compiled as freestanding [FIXME: ref?] - * with a command like:: + * whether the code MPS can be compiled as freestanding + * (design.mps.exec-env.req) with a command like:: * * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI \ - * --entry mps_lib_assert_fail mps.c mpslibfs.c vmfs.c + * --entry mps_lib_assert_fail mps.c mpslibfs.c * * .readership: For MPS client application developers and MPS developers. * .sources: @@ -60,6 +60,16 @@ mps_lib_assert_fail_t mps_lib_assert_fail_install(mps_lib_assert_fail_t handler) } +void *mps_lib_malloc(size_t size) +{ + NOTREACHED; +} + +void mps_lib_free(void *p) +{ + NOTREACHED; +} + void *(mps_lib_memset)(void *s, int c, size_t n) { NOTREACHED; diff --git a/code/vman.c b/code/vman.c index fba4afe7d5..4dc916b507 100644 --- a/code/vman.c +++ b/code/vman.c @@ -6,8 +6,7 @@ #include "mpm.h" #include "vm.h" - -#include /* for malloc and free */ +#include "mpslib.h" SRCID(vman, "$Id$"); @@ -58,7 +57,7 @@ Res VMInit(VM vm, Size size, Size grainSize, void *params) if (reserved < grainSize || reserved > (Size)(size_t)-1) return ResRESOURCE; - vbase = malloc((size_t)reserved); + vbase = mps_lib_malloc((size_t)reserved); if (vbase == NULL) return ResMEMORY; (void)mps_lib_memset(vbase, VMJunkBYTE, reserved); @@ -96,7 +95,7 @@ void VMFinish(VM vm) vm->sig = SigInvalid; (void)mps_lib_memset(vm->block, VMJunkBYTE, vm->reserved); - free(vm->block); + mps_lib_free(vm->block); } diff --git a/code/vmfs.c b/code/vmfs.c deleted file mode 100644 index c9f658c4cb..0000000000 --- a/code/vmfs.c +++ /dev/null @@ -1,93 +0,0 @@ -/* vmfs.c: FREESTANDING VM: VIRTUAL MEMORY MAPPING STUB - * - * $Id$ - * Copyright (c) 2001-2023 Ravenbrook Limited. See end of file for license. - * - * .purpose: The purpose of this code is to test at compile time - * whether the code MPS can be compiled as freestanding [FIXME: ref?] - * with a command like:: - * - * gcc -nostdlib -DCONFIG_PLINTH_NONE -DCONFIG_PF_ANSI \ - * --entry mps_lib_assert_fail mps.c mpslibfs.c vmfs.c - * - * .sources: design.mps.lib - */ - -#include "mpm.h" -#include "vm.h" - -SRCID(vmfs, "$Id$"); - - -Size PageSize(void) -{ - NOTREACHED; -} - - -Res VMParamFromArgs(void *params, size_t paramSize, ArgList args) -{ - NOTREACHED; -} - - -/* VMInit -- reserve some virtual address space, and create a VM structure */ - -Res VMInit(VM vm, Size size, Size grainSize, void *params) -{ - NOTREACHED; -} - - -/* VMFinish -- release all address space and finish VM structure */ - -void VMFinish(VM vm) -{ - NOTREACHED; -} - - -/* VMMap -- map the given range of memory */ - -Res VMMap(VM vm, Addr base, Addr limit) -{ - NOTREACHED; -} - - -/* VMUnmap -- unmap the given range of memory */ - -void VMUnmap(VM vm, Addr base, Addr limit) -{ - NOTREACHED; -} - - -/* C. COPYRIGHT AND LICENSE - * - * Copyright (C) 2001-2023 Ravenbrook Limited . - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the - * distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */