1.1 --- a/libc/Makefile Wed Oct 16 00:52:26 2019 +0200
1.2 +++ b/libc/Makefile Fri Oct 18 01:05:43 2019 +0200
1.3 @@ -57,7 +57,9 @@
1.4
1.5 # System integration sources.
1.6
1.7 -SOURCES_SYSTEM = l4re/syscalls.c l4re/sys_client.c
1.8 +SOURCES_SYSTEM = $(wildcard $(THISDIR)/l4re/*.c)
1.9 +
1.10 +SOURCES_SYSTEM_RELATIVE = $(patsubst $(THISDIR)/%,%,$(SOURCES_SYSTEM))
1.11
1.12 # Some stdio files are generated from the same file using different definitions.
1.13 # See the rules at the end for the generation of these files.
1.14 @@ -112,7 +114,7 @@
1.15 # generated files, plus the required libm files.
1.16
1.17 SRC_C = \
1.18 - $(SOURCES_SYSTEM) \
1.19 + $(SOURCES_SYSTEM_RELATIVE) \
1.20 $(filter-out \
1.21 $(SOURCES_DISABLED) \
1.22 $(SOURCES_DISCARDED), \
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/libc/l4re/sys_memory.c Fri Oct 18 01:05:43 2019 +0200
2.3 @@ -0,0 +1,115 @@
2.4 +/*
2.5 + * Traditional system functions for memory management.
2.6 + *
2.7 + * Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk>
2.8 + *
2.9 + * This program is free software; you can redistribute it and/or
2.10 + * modify it under the terms of the GNU General Public License as
2.11 + * published by the Free Software Foundation; either version 2 of
2.12 + * the License, or (at your option) any later version.
2.13 + *
2.14 + * This program is distributed in the hope that it will be useful,
2.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.17 + * GNU General Public License for more details.
2.18 + *
2.19 + * You should have received a copy of the GNU General Public License
2.20 + * along with this program; if not, write to the Free Software
2.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
2.22 + * Boston, MA 02110-1301, USA
2.23 + */
2.24 +
2.25 +#include <l4/sys/types.h>
2.26 +
2.27 +#include <stdlib.h>
2.28 +
2.29 +#include <ipc/mem_ipc.h>
2.30 +
2.31 +#include "sys_memory.h"
2.32 +
2.33 +
2.34 +
2.35 +/* NOTE: This should use the reentrancy state and prevent concurrent access. */
2.36 +
2.37 +/* The current and previous limit of the data segment. */
2.38 +
2.39 +static void *program_break = 0;
2.40 +static void *program_break_previous = 0;
2.41 +
2.42 +/* Available memory already allocated. */
2.43 +
2.44 +const unsigned long initial_memory_size = L4_PAGESIZE;
2.45 +unsigned long memory_available = 0;
2.46 +
2.47 +
2.48 +
2.49 +/* Return the "program break" or address of the first location after the end of
2.50 + the data segment. If increment is non-zero, allocate more data space and
2.51 + provide the address before allocation as the result. */
2.52 +
2.53 +void *sys_sbrk(intptr_t increment)
2.54 +{
2.55 + l4_cap_idx_t ds;
2.56 + void *addr;
2.57 + unsigned long size, requested;
2.58 +
2.59 + /* For a zero increment, obtain the current limit. */
2.60 +
2.61 + if (!increment)
2.62 + {
2.63 + /* Without any recorded address, obtain some memory first in order to provide
2.64 + the initial address. */
2.65 +
2.66 + if (!program_break)
2.67 + {
2.68 + if (ipc_allocate(initial_memory_size, &program_break_previous, &ds))
2.69 + return (void *) -1;
2.70 +
2.71 + if (ipc_dataspace_size(ds, &memory_available))
2.72 + return (void *) -1;
2.73 +
2.74 + /* The actual limit is concealed from the caller. */
2.75 +
2.76 + program_break = program_break_previous;
2.77 + }
2.78 + }
2.79 +
2.80 + /* For a non-zero increment, obtain a new limit, using any available memory
2.81 + from initialisation. */
2.82 +
2.83 + /* NOTE: For now, ignore negative increments. */
2.84 +
2.85 + else if (increment > 0)
2.86 + {
2.87 + program_break_previous = program_break;
2.88 +
2.89 + /* Consume available pre-allocated memory if possible. */
2.90 +
2.91 + requested = labs(increment);
2.92 +
2.93 + if (memory_available >= requested)
2.94 + {
2.95 + memory_available -= requested;
2.96 + program_break += requested;
2.97 + }
2.98 +
2.99 + /* Without any available pre-allocated memory, obtain a new dataspace and
2.100 + advance the address. */
2.101 +
2.102 + else
2.103 + {
2.104 + if (ipc_allocate(requested - memory_available, &addr, &ds))
2.105 + return (void *) -1;
2.106 +
2.107 + if (ipc_dataspace_size(ds, &size))
2.108 + return (void *) -1;
2.109 +
2.110 + program_break += size;
2.111 + memory_available = 0;
2.112 + }
2.113 + }
2.114 +
2.115 + /* Return the previous limit. */
2.116 +
2.117 + return program_break_previous;
2.118 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/libc/l4re/sys_memory.h Fri Oct 18 01:05:43 2019 +0200
3.3 @@ -0,0 +1,26 @@
3.4 +/*
3.5 + * Traditional system functions for memory management.
3.6 + *
3.7 + * Copyright (C) 2019 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 <unistd.h>
3.28 +
3.29 +void *sys_sbrk(intptr_t increment);
4.1 --- a/libc/l4re/syscalls.c Wed Oct 16 00:52:26 2019 +0200
4.2 +++ b/libc/l4re/syscalls.c Fri Oct 18 01:05:43 2019 +0200
4.3 @@ -30,7 +30,8 @@
4.4
4.5 #include <reent.h> /* struct _reent */
4.6
4.7 -#include "sys_client.h"
4.8 +#include "sys_client.h" /* close, open, read, write */
4.9 +#include "sys_memory.h" /* sbrk */
4.10
4.11 int _close_r(struct _reent *reent, int fd)
4.12 {
4.13 @@ -121,7 +122,8 @@
4.14
4.15 void *_sbrk_r(struct _reent *reent, intptr_t increment)
4.16 {
4.17 - return (void *) -1;
4.18 + /* NOTE: Not handling errno yet. */
4.19 + return sys_sbrk(increment);
4.20 }
4.21
4.22 int _stat_r(struct _reent *reent, const char *pathname, struct stat *buf)