summaryrefslogtreecommitdiff
path: root/nuttx/include/nuttx/addrenv.h
blob: 0cb854376f2754fdf8b5dade5201a218ac40abd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/****************************************************************************
 * include/nuttx/addrenv.h
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * 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.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * 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 OWNER 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.
 *
 ****************************************************************************/

#ifndef __INCLUDE_NUTTX_ADDRENV_H
#define __INCLUDE_NUTTX_ADDRENV_H 1

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#ifdef CONFIG_BUILD_KERNEL
#  include <signal.h>
#  include <nuttx/mm/mm.h>
#endif

#ifdef CONFIG_ARCH_ADDRENV

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
/* Configuration ************************************************************/
/* Pre-requisites */

#ifndef CONFIG_MM_PGALLOC
#  error CONFIG_MM_PGALLOC not defined
#endif

#ifndef CONFIG_MM_PGSIZE
#  error CONFIG_MM_PGSIZE not defined
#endif

/* .text region */

#ifndef CONFIG_ARCH_TEXT_VBASE
#  error CONFIG_ARCH_TEXT_VBASE not defined
#  define CONFIG_ARCH_TEXT_VBASE 0
#endif

#if (CONFIG_ARCH_TEXT_VBASE & CONFIG_MM_MASK) != 0
#  error CONFIG_ARCH_TEXT_VBASE not aligned to page boundary
#endif

#ifndef CONFIG_ARCH_TEXT_NPAGES
#  warning CONFIG_ARCH_TEXT_NPAGES not defined
#  define CONFIG_ARCH_TEXT_NPAGES 1
#endif

#define ARCH_TEXT_SIZE  (CONFIG_ARCH_TEXT_NPAGES * CONFIG_MM_PGSIZE)
#define ARCH_TEXT_VEND  (CONFIG_ARCH_TEXT_VBASE + ARCH_TEXT_SIZE)

/* .bss/.data region */

#ifndef CONFIG_ARCH_DATA_VBASE
#  error CONFIG_ARCH_DATA_VBASE not defined
#  define CONFIG_ARCH_DATA_VBASE ARCH_TEXT_VEND
#endif

#if (CONFIG_ARCH_DATA_VBASE & CONFIG_MM_MASK) != 0
#  error CONFIG_ARCH_DATA_VBASE not aligned to page boundary
#endif

#ifndef CONFIG_ARCH_DATA_NPAGES
#  warning CONFIG_ARCH_DATA_NPAGES not defined
#  define CONFIG_ARCH_DATA_NPAGES 1
#endif

#define ARCH_DATA_SIZE  (CONFIG_ARCH_DATA_NPAGES * CONFIG_MM_PGSIZE)
#define ARCH_DATA_VEND  (CONFIG_ARCH_DATA_VBASE + ARCH_DATA_SIZE)

/* Reserved .bss/.data region.  In the kernel build (CONFIG_BUILD_KERNEL),
 * the region at the beginning of the .bss/.data region is reserved for use
 * by the OS.  This reserved region contains support for:
 *
 *   1. The task group's heap memory management data structures and
 *   2. Support for delivery of signals.
 *
 * We don't use sizeof(struct addrenv_reserve_s) but, instead, a nice
 * even number that we must be assure is greater than or equal to
 * sizeof(struct addrenv_reserve_s)
 */

#ifdef CONFIG_BUILD_KERNEL
#  define ARCH_DATA_RESERVE_SIZE 512
#else
#  define ARCH_DATA_RESERVE_SIZE 0
#endif

/* Heap region */

#ifndef CONFIG_ARCH_HEAP_VBASE
#  error CONFIG_ARCH_HEAP_VBASE not defined
#  define CONFIG_ARCH_HEAP_VBASE ARCH_DATA_VEND
#endif

#if (CONFIG_ARCH_HEAP_VBASE & CONFIG_MM_MASK) != 0
#  error CONFIG_ARCH_HEAP_VBASE not aligned to page boundary
#endif

#ifndef CONFIG_ARCH_HEAP_NPAGES
#  warning CONFIG_ARCH_HEAP_NPAGES not defined
#  define CONFIG_ARCH_HEAP_NPAGES 1
#endif

#define ARCH_HEAP_SIZE  (CONFIG_ARCH_HEAP_NPAGES * CONFIG_MM_PGSIZE)
#define ARCH_HEAP_VEND  (CONFIG_ARCH_HEAP_VBASE + ARCH_HEAP_SIZE)

#ifdef CONFIG_ARCH_STACK_DYNAMIC
  /* User stack region */

#  ifndef CONFIG_ARCH_STACK_VBASE
#    error CONFIG_ARCH_STACK_VBASE not defined
#    define CONFIG_ARCH_STACK_VBASE ARCH_HEAP_VEND
#  endif

#  if (CONFIG_ARCH_STACK_VBASE & CONFIG_MM_MASK) != 0
#    error CONFIG_ARCH_STACK_VBASE not aligned to page boundary
#  endif

#  ifndef CONFIG_ARCH_STACK_NPAGES
#    warning CONFIG_ARCH_STACK_NPAGES not defined
#    define CONFIG_ARCH_STACK_NPAGES 1
#  endif

#  define ARCH_STACK_SIZE (CONFIG_ARCH_STACK_NPAGES * CONFIG_MM_PGSIZE)
#  define ARCH_STACK_VEND (CONFIG_ARCH_STACK_VBASE + ARCH_STACK_SIZE)

#ifdef CONFIG_ARCH_KERNEL_STACK
/* Kernel stack */

#  ifndef CONFIG_ARCH_KERNEL_STACKSIZE
#    define CONFIG_ARCH_KERNEL_STACKSIZE 1568
#  endif
#endif

  /* A single page scratch region used for temporary mappings */

#  define __ARCH_SHM_VBASE ARCH_STACK_VEND
#else
  /* A single page scratch region used for temporary mappings */

#  define __ARCH_SHM_VBASE ARCH_HEAP_VEND
#endif

/* Shared memory regions */

#ifdef CONFIG_MM_SHM
#  ifndef CONFIG_ARCH_SHM_VBASE
#    error CONFIG_ARCH_SHM_VBASE not defined
#    define CONFIG_ARCH_SHM_VBASE __ARCH_SHM_VBASE
#  endif

#  if (CONFIG_ARCH_SHM_VBASE & CONFIG_MM_MASK) != 0
#    error CONFIG_ARCH_SHM_VBASE not aligned to page boundary
#  endif

#  ifndef CONFIG_ARCH_SHM_MAXREGIONS
#    warning CONFIG_ARCH_SHM_MAXREGIONS not defined
#    define CONFIG_ARCH_SHM_MAXREGIONS 1
#  endif

#  ifndef CONFIG_ARCH_SHM_NPAGES
#    warning CONFIG_ARCH_SHM_NPAGES not defined
#    define CONFIG_ARCH_SHM_NPAGES 1
#  endif

#  define ARCH_SHM_MAXPAGES   (CONFIG_ARCH_SHM_NPAGES * CONFIG_ARCH_SHM_MAXREGIONS)
#  define ARCH_SHM_REGIONSIZE (CONFIG_ARCH_SHM_NPAGES * CONFIG_MM_PGSIZE)
#  define ARCH_SHM_SIZE       (CONFIG_ARCH_SHM_MAXREGIONS * ARCH_SHM_REGIONSIZE)
#  define ARCH_SHM_VEND       (CONFIG_ARCH_SHM_VBASE + ARCH_SHM_SIZE)

#  define ARCH_SCRATCH_VBASE   ARCH_SHM_VEND
#else
#  define ARCH_SCRATCH_VBASE   __ARCH_SHM_VBASE
#endif

/* There is no need to use the scratch memory region if the page pool memory
 * is statically mapped.
 */

#ifdef CONFIG_ARCH_PGPOOL_MAPPING

#  ifndef CONFIG_ARCH_PGPOOL_PBASE
#    error CONFIG_ARCH_PGPOOL_PBASE not defined
#  endif

#  ifndef CONFIG_ARCH_PGPOOL_VBASE
#    error CONFIG_ARCH_PGPOOL_VBASE not defined
#  endif

#  ifndef CONFIG_ARCH_PGPOOL_SIZE
#    error CONFIG_ARCH_PGPOOL_SIZE not defined
#  endif

#  define CONFIG_ARCH_PGPOOL_PEND \
     (CONFIG_ARCH_PGPOOL_PBASE + CONFIG_ARCH_PGPOOL_SIZE)
#  define CONFIG_ARCH_PGPOOL_VEND \
     (CONFIG_ARCH_PGPOOL_VBASE + CONFIG_ARCH_PGPOOL_SIZE)

#endif

/****************************************************************************
 * Public Types
 ****************************************************************************/

#ifndef __ASSEMBLY__

/* Reserved .bss/.data region.  In the kernel build (CONFIG_BUILD_KERNEL),
 * the region at the beginning of the .bss/.data region is reserved for use
 * by the OS.  This reserved region contains support for:
 *
 *   1. The task group's heap memory management data structures and
 *   2. Support for delivery of signals.
 */

#ifdef CONFIG_BUILD_KERNEL
#ifndef CONFIG_DISABLE_SIGNALS
/* This is the type of the signal handler trampoline routine.  This
 * trampoline is called directly from kernel logic.  It simply forwards the
 * signal information to the real signal handler.  When the signal handler
 * returns, this function issues a system call in order to return in kernel
 * mode.
 */

typedef void (*addrenv_sigtramp_t)(_sa_sigaction_t sighand, int signo,
                                   FAR siginfo_t *info, FAR void *ucontext);
#endif

/* This structure describes the format of the .bss/.data reserved area */

struct addrenv_reserve_s
{
#ifndef CONFIG_DISABLE_SIGNALS
  addrenv_sigtramp_t ar_sigtramp;  /* Signal trampoline */
#endif
  struct mm_heap_s   ar_usrheap;   /* User space heap structure */
};

/* Each instance of this structure resides at the beginning of the user-
 * space .bss/.data region.  This macro is provided to simplify access:
 */

#define ARCH_DATA_RESERVE \
  ((FAR struct addrenv_reserve_s *)CONFIG_ARCH_DATA_VBASE)
#endif

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Address Environment Interfaces
 *
 * Low-level interfaces used in binfmt/ to instantiate tasks with address
 * environments.  These interfaces all operate on type group_addrenv_t which
 * is an abstract representation of a task group's address environment and
 * must be defined in arch/arch.h if CONFIG_ARCH_ADDRENV is defined.
 *
 *   up_addrenv_create   - Create an address environment
 *   up_addrenv_destroy  - Destroy an address environment.
 *   up_addrenv_vtext    - Returns the virtual base address of the .text
 *                         address environment
 *   up_addrenv_vdata    - Returns the virtual base address of the .bss/.data
 *                         address environment
 *   up_addrenv_heapsize - Returns the size of the initial heap allocation.
 *   up_addrenv_select   - Instantiate an address environment
 *   up_addrenv_restore  - Restore an address environment
 *   up_addrenv_clone    - Copy an address environment from one location to
 *                         another.
 *
 * Higher-level interfaces used by the tasking logic.  These interfaces are
 * used by the functions in sched/ and all operate on the thread which whose
 * group been assigned an address environment by up_addrenv_clone().
 *
 *   up_addrenv_attach   - Clone the address environment assigned to one TCB
 *                         to another.  This operation is done when a pthread
 *                         is created that share's the same group address
 *                         environment.
 *   up_addrenv_detach   - Release the thread's reference to an address
 *                         environment when a task/thread exits.
 *
 * CONFIG_ARCH_STACK_DYNAMIC=y indicates that the user process stack resides
 * in its own address space.  This options is also *required* if
 * CONFIG_BUILD_KERNEL and CONFIG_LIBC_EXECFUNCS are selected.  Why?
 * Because the caller's stack must be preserved in its own address space
 * when we instantiate the environment of the new process in order to
 * initialize it.
 *
 * NOTE: The naming of the CONFIG_ARCH_STACK_DYNAMIC selection implies that
 * dynamic stack allocation is supported.  Certainly this option must be set
 * if dynamic stack allocation is supported by a platform.  But the more
 * general meaning of this configuration environment is simply that the
 * stack has its own address space.
 *
 * If CONFIG_ARCH_STACK_DYNAMIC=y is selected then the platform specific
 * code must export these additional interfaces:
 *
 *   up_addrenv_ustackalloc  - Create a stack address environment
 *   up_addrenv_ustackfree   - Destroy a stack address environment.
 *   up_addrenv_vustack      - Returns the virtual base address of the stack
 *   up_addrenv_ustackselect - Instantiate a stack address environment
 *
 * If CONFIG_ARCH_KERNEL_STACK is selected, then each user process will have
 * two stacks:  (1) a large (and possibly dynamic) user stack and (2) a
 * smaller kernel stack.  However, this option is *required* if both
 * CONFIG_BUILD_KERNEL and CONFIG_LIBC_EXECFUNCS are selected.  Why?  Because
 * when we instantiate and initialize the address environment of the new
 * user process, we will temporarily lose the address environment of the old
 * user process, including its stack contents.  The kernel C logic will crash
 * immediately with no valid stack in place.
 *
 * If CONFIG_ARCH_STACK_DYNAMIC=y is selected then the platform specific
 * code must export these additional interfaces:
 *
 *   up_addrenv_kstackalloc  - Create a stack in the kernel address environment
 *   up_addrenv_kstackfree   - Destroy the kernel stack.
 *
 ****************************************************************************/

/* Prototyped in include/nuttx/arch.h as part of the OS/platform interface */

#endif /* __ASSEMBLY__ */
#endif /* CONFIG_ARCH_ADDRENV */
#endif /* __INCLUDE_NUTTX_ADDRENV_H */