Xenomai  3.1
internal.h
1 /*
2  * Copyright (C) 2011 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _COPPERPLATE_INTERNAL_H
19 #define _COPPERPLATE_INTERNAL_H
20 
21 #include <sys/types.h>
22 #include <stdarg.h>
23 #include <time.h>
24 #include <pthread.h>
25 #include <semaphore.h>
26 #include <xeno_config.h>
27 #include <boilerplate/list.h>
28 #include <boilerplate/ancillaries.h>
29 #include <boilerplate/limits.h>
30 #include <boilerplate/sched.h>
31 #include <boilerplate/setup.h>
32 #include <copperplate/heapobj.h>
33 #include <copperplate/tunables.h>
34 
35 #ifdef CONFIG_XENO_PSHARED
36 
37 #include <boilerplate/shavl.h>
38 
39 #define SHEAPMEM_PAGE_SHIFT 9 /* 2^9 => 512 bytes */
40 #define SHEAPMEM_PAGE_SIZE (1UL << SHEAPMEM_PAGE_SHIFT)
41 #define SHEAPMEM_PAGE_MASK (~(SHEAPMEM_PAGE_SIZE - 1))
42 #define SHEAPMEM_MIN_LOG2 4 /* 16 bytes */
43 /*
44  * Use bucketed memory for sizes between 2^SHEAPMEM_MIN_LOG2 and
45  * 2^(SHEAPMEM_PAGE_SHIFT-1).
46  */
47 #define SHEAPMEM_MAX (SHEAPMEM_PAGE_SHIFT - SHEAPMEM_MIN_LOG2)
48 #define SHEAPMEM_MIN_ALIGN (1U << SHEAPMEM_MIN_LOG2)
49 /* Max size of an extent (4Gb - SHEAPMEM_PAGE_SIZE). */
50 #define SHEAPMEM_MAX_EXTSZ (4294967295U - SHEAPMEM_PAGE_SIZE + 1)
51 /* Bits we need for encoding a page # */
52 #define SHEAPMEM_PGENT_BITS (32 - SHEAPMEM_PAGE_SHIFT)
53 
54 /* Each page is represented by a page map entry. */
55 #define SHEAPMEM_PGMAP_BYTES sizeof(struct sheapmem_pgentry)
56 
57 struct sheapmem_pgentry {
58  /* Linkage in bucket list. */
59  unsigned int prev : SHEAPMEM_PGENT_BITS;
60  unsigned int next : SHEAPMEM_PGENT_BITS;
61  /* page_list or log2. */
62  unsigned int type : 6;
63  /*
64  * We hold either a spatial map of busy blocks within the page
65  * for bucketed memory (up to 32 blocks per page), or the
66  * overall size of the multi-page block if entry.type ==
67  * page_list.
68  */
69  union {
70  uint32_t map;
71  uint32_t bsize;
72  };
73 };
74 
75 /*
76  * A range descriptor is stored at the beginning of the first page of
77  * a range of free pages. sheapmem_range.size is nrpages *
78  * SHEAPMEM_PAGE_SIZE. Ranges are indexed by address and size in AVL
79  * trees.
80  */
81 struct sheapmem_range {
82  struct shavlh addr_node;
83  struct shavlh size_node;
84  size_t size;
85 };
86 
87 struct sheapmem_extent {
88  struct holder next;
89  memoff_t membase; /* Base offset of page array */
90  memoff_t memlim; /* Offset limit of page array */
91  struct shavl addr_tree;
92  struct shavl size_tree;
93  struct sheapmem_pgentry pagemap[0]; /* Start of page entries[] */
94 };
95 
96 #define __SHEAPMEM_MAP_SIZE(__nrpages) \
97  ((__nrpages) * SHEAPMEM_PGMAP_BYTES)
98 
99 #define __SHEAPMEM_ARENA_SIZE(__size) \
100  (__size + \
101  __align_to(sizeof(struct sheapmem_extent) + \
102  __SHEAPMEM_MAP_SIZE((__size) >> SHEAPMEM_PAGE_SHIFT), \
103  SHEAPMEM_MIN_ALIGN))
104 
105 /*
106  * Calculate the minimal size of the memory arena needed to contain a
107  * heap of __user_size bytes, including our meta data for managing it.
108  * Usable at build time if __user_size is constant.
109  */
110 #define SHEAPMEM_ARENA_SIZE(__user_size) \
111  __SHEAPMEM_ARENA_SIZE(__align_to(__user_size, SHEAPMEM_PAGE_SIZE))
112 
113 /*
114  * The struct below has to live in shared memory; no direct reference
115  * to process local memory in there.
116  */
117 struct shared_heap_memory {
118  char name[XNOBJECT_NAME_LEN];
119  pthread_mutex_t lock;
120  struct listobj extents;
121  size_t arena_size;
122  size_t usable_size;
123  size_t used_size;
124  /* Heads of page lists for log2-sized blocks. */
125  uint32_t buckets[SHEAPMEM_MAX];
126  struct sysgroup_memspec memspec;
127 };
128 
129 ssize_t sheapmem_check(struct shared_heap_memory *heap, void *block);
130 
131 #endif /* CONFIG_XENO_PSHARED */
132 
133 #ifdef CONFIG_XENO_REGISTRY
134 #define DEFAULT_REGISTRY_ROOT CONFIG_XENO_REGISTRY_ROOT
135 #else
136 #define DEFAULT_REGISTRY_ROOT NULL
137 #endif
138 
139 struct corethread_attributes {
140  size_t stacksize;
141  int detachstate;
142  int policy;
143  struct sched_param_ex param_ex;
144  int (*prologue)(void *arg);
145  void *(*run)(void *arg);
146  void *arg;
147  struct {
148  int status;
149  sem_t warm;
150  sem_t *released;
151  } __reserved;
152 };
153 
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 void copperplate_set_current_name(const char *name);
159 
160 int copperplate_get_current_name(char *name, size_t maxlen);
161 
162 int copperplate_kill_tid(pid_t tid, int sig);
163 
164 int copperplate_probe_tid(pid_t tid);
165 
166 int copperplate_create_thread(struct corethread_attributes *cta,
167  pthread_t *ptid);
168 
169 int copperplate_renice_local_thread(pthread_t ptid, int policy,
170  const struct sched_param_ex *param_ex);
171 
172 void copperplate_bootstrap_internal(const char *arg0,
173  char *mountpt, int regflags);
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #endif /* _COPPERPLATE_INTERNAL_H */