1.1 --- a/include/ipc/cap_alloc.h Sun Feb 14 01:06:09 2021 +0100
1.2 +++ b/include/ipc/cap_alloc.h Sat Feb 27 23:36:13 2021 +0100
1.3 @@ -25,9 +25,7 @@
1.4
1.5 EXTERN_C_BEGIN
1.6
1.7 -long ipc_cap_alloc_init(void);
1.8 l4_cap_idx_t ipc_cap_alloc(void);
1.9 -
1.10 int ipc_cap_free(l4_cap_idx_t cap);
1.11 int ipc_cap_free_um(l4_cap_idx_t cap);
1.12
2.1 --- a/include/ipc/mem_ipc.h Sun Feb 14 01:06:09 2021 +0100
2.2 +++ b/include/ipc/mem_ipc.h Sat Feb 27 23:36:13 2021 +0100
2.3 @@ -1,7 +1,7 @@
2.4 /*
2.5 * Memory sharing abstractions.
2.6 *
2.7 - * Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk>
2.8 + * Copyright (C) 2018, 2019, 2021 Paul Boddie <paul@boddie.org.uk>
2.9 *
2.10 * This program is free software; you can redistribute it and/or
2.11 * modify it under the terms of the GNU General Public License as
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/include/ipc/thread.h Sat Feb 27 23:36:13 2021 +0100
3.3 @@ -0,0 +1,31 @@
3.4 +/*
3.5 + * Thread-related initialisation.
3.6 + *
3.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
3.8 + *
3.9 + * This program is free software; you can redistribute it and/or
3.10 + * modify it under the terms of the GNU General Public License as
3.11 + * published by the Free Software Foundation; either version 2 of
3.12 + * the License, or (at your option) any later version.
3.13 + *
3.14 + * This program is distributed in the hope that it will be useful,
3.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.17 + * GNU General Public License for more details.
3.18 + *
3.19 + * You should have received a copy of the GNU General Public License
3.20 + * along with this program; if not, write to the Free Software
3.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
3.22 + * Boston, MA 02110-1301, USA
3.23 + */
3.24 +
3.25 +#pragma once
3.26 +
3.27 +#include <l4/sys/types.h>
3.28 +
3.29 +EXTERN_C_BEGIN
3.30 +
3.31 +long ipc_cap_alloc_init(void);
3.32 +long ipc_mem_init(void);
3.33 +
3.34 +EXTERN_C_END
4.1 --- a/lib/src/Makefile Sun Feb 14 01:06:09 2021 +0100
4.2 +++ b/lib/src/Makefile Sat Feb 27 23:36:13 2021 +0100
4.3 @@ -27,7 +27,7 @@
4.4
4.5 PLAIN_SRC_C = \
4.6 cap_alloc.c direct.c irq.c mem_ipc.c message.c \
4.7 - server.c sys_memory.c util_ipc.c \
4.8 + server.c sys_memory.c thread.c util_ipc.c \
4.9
4.10 # Normal definitions.
4.11
5.1 --- a/lib/src/cap_alloc.c Sun Feb 14 01:06:09 2021 +0100
5.2 +++ b/lib/src/cap_alloc.c Sat Feb 27 23:36:13 2021 +0100
5.3 @@ -30,6 +30,7 @@
5.4 * boilerplate, standard C++ library and, inevitably, C library functionality.
5.5 */
5.6
5.7 +#include <l4/re/c/util/cap_alloc.h>
5.8 #include <l4/re/env.h>
5.9 #include <l4/sys/consts.h>
5.10 #include <l4/sys/factory.h>
5.11 @@ -67,30 +68,11 @@
5.12
5.13
5.14
5.15 -static l4_cap_idx_t semaphore = L4_INVALID_CAP;
5.16 -
5.17 -/* Bootstrap the semaphore support needed by the allocation function. */
5.18 -
5.19 -long ipc_cap_alloc_init()
5.20 -{
5.21 - if (l4_is_invalid_cap(semaphore))
5.22 - {
5.23 - semaphore = ipc_cap_alloc();
5.24 +/* Semaphore used if threading has been initialised by ipc_cap_alloc_init. */
5.25
5.26 - if (l4_is_invalid_cap(semaphore))
5.27 - return -L4_ENOENT;
5.28 +l4_cap_idx_t ipc_cap_alloc_semaphore = L4_INVALID_CAP;
5.29
5.30 - if (l4_error(l4_factory_create(l4re_env()->factory, L4_PROTO_SEMAPHORE, semaphore)))
5.31 - {
5.32 - ipc_cap_free_um(semaphore);
5.33 - return -L4_ENOENT;
5.34 - }
5.35
5.36 - l4_semaphore_up(semaphore);
5.37 - }
5.38 -
5.39 - return L4_EOK;
5.40 -}
5.41
5.42 /* Reserve and return a capability index. */
5.43
5.44 @@ -152,13 +134,13 @@
5.45
5.46 /* Allocate a capability in the critical section, then return. */
5.47
5.48 - if (l4_is_valid_cap(semaphore))
5.49 - l4_semaphore_down(semaphore, L4_IPC_NEVER);
5.50 + if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
5.51 + l4_semaphore_down(ipc_cap_alloc_semaphore, L4_IPC_NEVER);
5.52
5.53 cap = _ipc_cap_alloc();
5.54
5.55 - if (l4_is_valid_cap(semaphore))
5.56 - l4_semaphore_up(semaphore);
5.57 + if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
5.58 + l4_semaphore_up(ipc_cap_alloc_semaphore);
5.59
5.60 return cap;
5.61 }
5.62 @@ -203,8 +185,8 @@
5.63
5.64 result = _ipc_cap_free(cap);
5.65
5.66 - if (l4_is_valid_cap(semaphore))
5.67 - l4_semaphore_up(semaphore);
5.68 + if (l4_is_valid_cap(ipc_cap_alloc_semaphore))
5.69 + l4_semaphore_up(ipc_cap_alloc_semaphore);
5.70
5.71 return result;
5.72 }
6.1 --- a/lib/src/mem_ipc.c Sun Feb 14 01:06:09 2021 +0100
6.2 +++ b/lib/src/mem_ipc.c Sat Feb 27 23:36:13 2021 +0100
6.3 @@ -23,7 +23,9 @@
6.4 #include <l4/re/protocols.h>
6.5 #include <l4/re/c/rm.h>
6.6 #include <l4/sys/err.h>
6.7 +#include <l4/sys/semaphore.h>
6.8 #include <l4/sys/task.h>
6.9 +#include <l4/sys/types.h>
6.10
6.11 #include "cap_alloc.h"
6.12 #include "mem_ipc.h"
6.13 @@ -35,6 +37,11 @@
6.14 #include "region_mapper_client.h"
6.15
6.16
6.17 +/* Semaphore used if threading has been initialised by ipc_mem_init. */
6.18 +
6.19 +l4_cap_idx_t ipc_mem_semaphore = L4_INVALID_CAP;
6.20 +
6.21 +
6.22
6.23 /* Generic memory allocation. */
6.24
6.25 @@ -70,24 +77,38 @@
6.26
6.27 long ipc_attach_dataspace(l4_cap_idx_t cap, unsigned long size, void **addr)
6.28 {
6.29 + long err;
6.30 +
6.31 + if (l4_is_valid_cap(ipc_mem_semaphore))
6.32 + l4_semaphore_down(ipc_mem_semaphore, L4_IPC_NEVER);
6.33 +
6.34 /* Attach a region starting at or after start/addr, offset is zero. */
6.35
6.36 - return RegionMapper_attach((ref_RegionMapper) l4re_env()->rm,
6.37 - (l4_addr_t *) addr, size, L4RE_RM_SEARCH_ADDR, cap,
6.38 - 0, L4_PAGESHIFT);
6.39 + err = RegionMapper_attach((ref_RegionMapper) l4re_env()->rm,
6.40 + (l4_addr_t *) addr, size, L4RE_RM_SEARCH_ADDR, cap,
6.41 + 0, L4_PAGESHIFT);
6.42 +
6.43 + if (l4_is_valid_cap(ipc_mem_semaphore))
6.44 + l4_semaphore_up(ipc_mem_semaphore);
6.45 +
6.46 + return err;
6.47 }
6.48
6.49 -/* Detach a dataspace region. Equivalent to l4re_rm_detach. */
6.50 +/* Detach a dataspace region. Equivalent to l4re_rm_detach_unmap. */
6.51
6.52 long ipc_detach_dataspace(void *addr)
6.53 {
6.54 l4_cap_idx_t ds;
6.55 l4_addr_t start = (l4_addr_t) addr;
6.56 unsigned long size;
6.57 + long err;
6.58 +
6.59 + if (l4_is_valid_cap(ipc_mem_semaphore))
6.60 + l4_semaphore_down(ipc_mem_semaphore, L4_IPC_NEVER);
6.61
6.62 /* Detach a region of at least size 1, flags 2 (overlap). */
6.63
6.64 - long err = RegionMapper_detach((ref_RegionMapper) l4re_env()->rm, start, 1, 2,
6.65 + err = RegionMapper_detach((ref_RegionMapper) l4re_env()->rm, start, 1, 2,
6.66 &start, &size, &ds);
6.67
6.68 /* Unmap flexpages for the region if successful. */
6.69 @@ -95,6 +116,9 @@
6.70 if (!err)
6.71 ipc_unmap_region(start, size);
6.72
6.73 + if (l4_is_valid_cap(ipc_mem_semaphore))
6.74 + l4_semaphore_up(ipc_mem_semaphore);
6.75 +
6.76 return err;
6.77 }
6.78
6.79 @@ -129,3 +153,5 @@
6.80 start += (1UL << order);
6.81 }
6.82 }
6.83 +
6.84 +// vim: tabstop=2 expandtab shiftwidth=2
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/lib/src/thread.c Sat Feb 27 23:36:13 2021 +0100
7.3 @@ -0,0 +1,73 @@
7.4 +/*
7.5 + * Thread-related initialisation activities.
7.6 + *
7.7 + * Copyright (C) 2021 Paul Boddie <paul@boddie.org.uk>
7.8 + *
7.9 + * This program is free software; you can redistribute it and/or
7.10 + * modify it under the terms of the GNU General Public License as
7.11 + * published by the Free Software Foundation; either version 2 of
7.12 + * the License, or (at your option) any later version.
7.13 + *
7.14 + * This program is distributed in the hope that it will be useful,
7.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7.17 + * GNU General Public License for more details.
7.18 + *
7.19 + * You should have received a copy of the GNU General Public License
7.20 + * along with this program; if not, write to the Free Software
7.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
7.22 + * Boston, MA 02110-1301, USA
7.23 + */
7.24 +
7.25 +#include <l4/re/c/util/cap_alloc.h>
7.26 +#include <l4/re/env.h>
7.27 +#include <l4/sys/factory.h>
7.28 +#include <l4/sys/semaphore.h>
7.29 +
7.30 +#include "cap_alloc.h"
7.31 +#include "thread.h"
7.32 +
7.33 +
7.34 +
7.35 +/* Bootstrap the semaphore support needed by the allocation function. */
7.36 +
7.37 +static long ipc_semaphore_init(l4_cap_idx_t *semaphore)
7.38 +{
7.39 + if (l4_is_invalid_cap(*semaphore))
7.40 + {
7.41 + *semaphore = l4re_util_cap_alloc();
7.42 +
7.43 + if (l4_is_invalid_cap(*semaphore))
7.44 + return -L4_ENOENT;
7.45 +
7.46 + if (l4_error(l4_factory_create(l4re_env()->factory, L4_PROTO_SEMAPHORE, *semaphore)))
7.47 + {
7.48 + ipc_cap_free_um(*semaphore);
7.49 + return -L4_ENOENT;
7.50 + }
7.51 +
7.52 + l4_semaphore_up(*semaphore);
7.53 + }
7.54 +
7.55 + return L4_EOK;
7.56 +}
7.57 +
7.58 +/* Bootstrap the semaphore support needed by the allocation functions. */
7.59 +
7.60 +extern l4_cap_idx_t ipc_cap_alloc_semaphore;
7.61 +
7.62 +long ipc_cap_alloc_init()
7.63 +{
7.64 + return ipc_semaphore_init(&ipc_cap_alloc_semaphore);
7.65 +}
7.66 +
7.67 +/* Bootstrap the semaphore support needed by the memory functions. */
7.68 +
7.69 +extern l4_cap_idx_t ipc_mem_semaphore;
7.70 +
7.71 +long ipc_mem_init()
7.72 +{
7.73 + return ipc_semaphore_init(&ipc_mem_semaphore);
7.74 +}
7.75 +
7.76 +// vim: tabstop=2 expandtab shiftwidth=2