2 * This file is part of DDE/Linux2.6.
4 * (c) 2006-2010 Bjoern Doebel <doebel@os.inf.tu-dresden.de>
5 * Christian Helmuth <ch12@os.inf.tu-dresden.de>
6 * economic rights: Technische Universitaet Dresden (Germany)
8 * This file is part of TUD:OS and distributed under the terms of the
9 * GNU General Public License 2.
10 * Please see the COPYING-GPL-2 file for details.
14 * \brief kmalloc() implementation
16 * In Linux 2.6 this resides in mm/slab.c.
18 * This implementation of kmalloc() stays with Linux's and uses kmem_caches for
19 * some power of two bytes. For larger allocations ddedkit_large_malloc() is
20 * used. This way, we optimize for speed and potentially waste memory
25 #include <linux/slab.h>
26 #include <linux/types.h>
27 #include <linux/bootmem.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
34 #include <l4/dde/ddekit/debug.h>
35 #include <l4/dde/ddekit/memory.h>
37 #include <l4/dde/linux26/dde26.h>
42 /* This stuff is needed by some drivers, e.g. for ethtool.
43 * XXX: This is a fake, implement it if you really need ethtool stuff.
45 struct page* mem_map = NULL;
46 static bootmem_data_t contig_bootmem_data;
47 struct pglist_data contig_page_data = { .bdata = &contig_bootmem_data };
49 int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
50 unsigned long pfn, unsigned long size, pgprot_t prot)
54 EXPORT_SYMBOL(remap_pfn_range);
60 #define DEBUG_MALLOC 0
67 * These are the default caches for kmalloc. Custom caches can have other sizes.
69 static struct cache_sizes malloc_sizes[] = {
70 #define CACHE(x) { .cs_size = (x) },
71 #include <linux/kmalloc_sizes.h>
78 * kmalloc() cache names
80 static const char *malloc_names[] = {
81 #define CACHE(x) "size-" #x,
82 #include <linux/kmalloc_sizes.h>
89 * Find kmalloc() cache for size
91 static struct kmem_cache *find_cache(size_t size)
93 struct cache_sizes *sizes;
95 for (sizes = malloc_sizes; size > sizes->cs_size; ++sizes) ;
97 return sizes->cs_cachep;
102 * Free previously allocated memory
103 * @objp: pointer returned by kmalloc.
105 * If @objp is NULL, no operation is performed.
107 * Don't free memory not originally allocated by kmalloc()
108 * or you will run into trouble.
110 void kfree(const void *objp)
114 /* find cache back-pointer */
115 void **p = (void **)objp - 1;
117 ddekit_log(DEBUG_MALLOC, "objp=%p cache=%p (%d)",
118 p, *p, *p ? kmem_cache_size(*p) : 0);
121 /* free from cache */
122 kmem_cache_free(*p, p);
124 /* no cache for this size - use ddekit free */
125 ddekit_large_free(p);
131 * @size: how many bytes of memory are required.
132 * @flags: the type of memory to allocate.
134 * kmalloc is the normal method of allocating memory
137 void *__kmalloc(size_t size, gfp_t flags)
139 /* add space for back-pointer */
140 size += sizeof(void *);
142 /* find appropriate cache */
143 struct kmem_cache *cache = find_cache(size);
147 /* allocate from cache */
148 p = kmem_cache_alloc(cache, flags);
150 /* no cache for this size - use ddekit malloc */
151 p = ddekit_large_malloc(size);
153 ddekit_log(DEBUG_MALLOC, "size=%d, cache=%p (%d) => %p",
154 size, cache, cache ? kmem_cache_size(cache) : 0, p);
156 /* return pointer to actual chunk */
162 /* Need to zero out mem? */
163 if (p && (flags & __GFP_ZERO)) {
164 size -= sizeof(void*);
172 size_t ksize(const void *p)
174 struct kmem_cache *cache = (struct kmem_cache *)*((void**)p - 1);
176 return kmem_cache_size(cache);
181 void *dma_alloc_coherent(struct device *dev, size_t size,
182 dma_addr_t *dma_handle, gfp_t flag)
184 void *ret = (void *)__get_free_pages(flag, get_order(size));
187 memset(ret, 0, size);
188 *dma_handle = virt_to_bus(ret);
194 void dma_free_coherent(struct device *dev, size_t size,
195 void *vaddr, dma_addr_t dma_handle)
197 free_pages((unsigned long)vaddr, get_order(size));
201 #include <l4/sys/cache.h>
202 void dma_cache_maint(const void *start, size_t size, int direction)
204 l4_cache_dma_coherent((unsigned long)start, (unsigned long)start + size);
209 /********************
211 ********************/
214 * dde_linux kmalloc initialization
216 void l4dde26_kmalloc_init(void)
218 struct cache_sizes *sizes = malloc_sizes;
219 const char **names = malloc_names;
221 /* init malloc sizes array */
222 for (; sizes->cs_size != ULONG_MAX; ++sizes, ++names)
223 sizes->cs_cachep = kmem_cache_create(*names, sizes->cs_size, 0, 0, 0);