summaryrefslogtreecommitdiff
path: root/libddekit/memory.c
blob: 6602ea37e1e0e8d92f46a10d200efb6e0bbd7a57 (plain)
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
/*
 * \brief   Memory subsystem
 * \author  Thomas Friebel <tf13@os.inf.tu-dresden.de>
 * \author  Christian Helmuth <ch12@os.inf.tu-dresden.de>
 * \date    2006-11-03
 *
 * The memory subsystem provides the backing store for DMA-able memory via
 * large malloc and slabs.
 *
 * FIXME check thread-safety and add locks where appropriate
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <string.h>
#include <sys/mman.h>

#include "mach_U.h"
#include <hurd/slab.h>

#include "util.h"
#include "ddekit/memory.h"
#include "ddekit/panic.h"
#include "ddekit/pgtab.h"

#define CACHE_LINE_SIZE	64

/****************
 ** Page cache **
 ****************/

/* FIXME revisit two-linked-lists approach - we could get rid of the
 * pcache_free lists, because these descriptors can also be allocated
 * whenever the need arises. (XXX: Check how this hurts performance.)
 */

/*****************************************************************************
  DDEKit maintains a list of free pages that can be used for growing
  existing DDEKit slabs. We distinguish between 2 types of pages: the ones
  that have been allocated from physically contiguous dataspaces and the the
  ones that have not.

  For each type of cache, we maintain two lists: a list of free pages that
  can be used to grow a cache (pcache_used) and a list of free pcache entries
  that can be used to store pages when they get freed (pcache_free). The
  reason for this is that by using these two lists, cache operations can be
  done atomically using cmpxchg.

  The function ddekit_slab_setup_page_cache() is used to tune the number of
  cached pages.
 ******************************************************************************/

/* page cache to minimize allocations from external servers */
struct ddekit_pcache
{
	struct ddekit_pcache *next;
	void *page;
	int contig;
};

/**
 * Setup page cache for all slabs
 *
 * \param pages  maximal number of memory pages
 *
 * If the maximal number of unused pages is exceeded, subsequent deallocation
 * will be freed at the memory server. This page cache caches pages from all
 * slabs.
 */
void ddekit_slab_setup_page_cache(unsigned pages)
{
	UNIMPL;
}

/*******************************
 ** Slab cache implementation **
 *******************************/

/* ddekit slab facilitates l4slabs */
struct ddekit_slab
{
  struct hurd_slab_space space;
};

/**
 * Allocate object in slab
 */
void *ddekit_slab_alloc(struct ddekit_slab * slab)
{
	void *buffer;
	error_t err = hurd_slab_alloc (&slab->space, &buffer);
	return err ? NULL : buffer;
}


/**
 * Free object in slab
 */
void  ddekit_slab_free(struct ddekit_slab * slab, void *objp)
{
	hurd_slab_dealloc (&slab->space, objp);
}


/**
 * Store user pointer in slab cache
 */
void  ddekit_slab_set_data(struct ddekit_slab * slab, void *data)
{
#if 0
	l4slab_set_data(&slab->cache, data);
#endif
	UNIMPL;
}


/**
 * Read user pointer from slab cache
 */
void *ddekit_slab_get_data(struct ddekit_slab * slab)
{
#if 0
	return l4slab_get_data(&slab->cache);
#endif
	UNIMPL;
	return NULL;
}


/**
 * Destroy slab cache
 *
 * \param slab  pointer to slab cache structure
 */
void  ddekit_slab_destroy (struct ddekit_slab * slab)
{
	hurd_slab_free ((hurd_slab_space_t) slab);
}

error_t allocate_buffer (void *hook, size_t size, void **ptr)
{
	*ptr = ddekit_large_malloc (size);
	if (*ptr == NULL)
	  return ENOMEM;
	return 0;
}

error_t deallocate_buffer (void *hook, void *buffer, size_t size)
{
	ddekit_large_free (buffer);
	return 0;
}

/**
 * Initialize slab cache
 *
 * \param size          size of cache objects
 * \param contiguous    make this slab use physically contiguous memory
 *
 * \return pointer to new slab cache or 0 on error
 */
struct ddekit_slab * ddekit_slab_init(unsigned size, int contiguous)
{
	struct ddekit_slab * slab;
	error_t err;

	if (contiguous)
	  err = hurd_slab_create (size, CACHE_LINE_SIZE, allocate_buffer,
				  deallocate_buffer, NULL, NULL, NULL,
				  (hurd_slab_space_t *) &slab);
	else
	  /* If the object isn't used by DMA,
	   * we can use all default settings. */
	  err = hurd_slab_create (size, 0, NULL, NULL, NULL, NULL, NULL,
				  (hurd_slab_space_t *) &slab);
	if (err)
	  {
	    error (2, err, "hurd_slab_create");
	  }
	return slab;
}


/**********************************
 ** Large block memory allocator **
 **********************************/

/**
 * Free large block of memory
 *
 * This is no useful for allocation < page size.
 *
 * TODO The freed memory can be cached and will be still accessible (
 * no page fault when accessed). I hope it won't caused any troubles.
 */
void ddekit_large_free(void *objp)
{
  int err;
  int size = ddekit_pgtab_get_size (objp);
  ddekit_pgtab_clear_region (objp, 0);
  err = munmap (objp, size);
  if (err < 0)
    error (0, errno, "munmap");
}


/**
 * Allocate large block of memory
 *
 * This is no useful for allocation < page size.
 */
void *ddekit_large_malloc(int size)
{
  error_t err;
  vm_address_t vstart, pstart;
  extern mach_port_t priv_host;

  /* Allocate memory.  */
  err = vm_allocate_contiguous (priv_host, mach_task_self (),
			   &vstart, &pstart, size);
  if (err)
    {
      error (0, err, "vm_allocate_contiguous");
      vstart = 0;
    }
  else
    ddekit_pgtab_set_region_with_size ((void *) vstart, pstart, size, 0);

  return (void *) vstart;
}


/**
 * Allocate large block of memory (special interface)
 *
 * This is no useful for allocation < page size.
 *
 * FIXME implementation missing...
 */
void *ddekit_contig_malloc(unsigned long size,
                           unsigned long low, unsigned long high,
                           unsigned long alignment, unsigned long boundary)
{
#if 0
	void *res;
	int err;
	int pages;
	l4_size_t tmp;
	l4dm_mem_addr_t dm_paddr;

	size  = l4_round_page(size);
	pages = size >> L4_PAGESHIFT;

	res = l4dm_mem_allocate_named(size, L4DM_CONTIGUOUS | L4DM_PINNED | L4RM_MAP | L4RM_LOG2_ALIGNED);

	if (res) {
		/* check if res meets low/high/alignment/boundary
		 * and panic if it is not the case
		 * XXXY
		 */

		/* get physical address */
		err = l4dm_mem_phys_addr(res, 1, &dm_paddr, 1, &tmp);
		if (err != 1)
			ddekit_debug("contigmalloc: error getting physical address of new page!\n");
	
		/* set PTE */
		ddekit_set_ptes(res, dm_paddr.addr, pages, PTE_TYPE_CONTIG);
	}

	return res;
#else
	ddekit_debug("%s: not implemented\n", __func__);
	return 0;
#endif
}