1/**
2 * \file
3 * \brief Memobj definitions
4 */
5
6/*
7 * Copyright (c) 2009, 2010, 2011, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef LIBBARRELFISH_MEMOBJ_H
16#define LIBBARRELFISH_MEMOBJ_H
17
18#include <barrelfish/slab.h>
19#include <sys/cdefs.h>
20
21__BEGIN_DECLS
22
23// FIXME: these enum names need to be scoped (e.g. MEMOBJ_X rather than X) -AB
24enum memobj_type {
25    ANONYMOUS,
26    ONE_FRAME,
27    ONE_FRAME_LAZY,
28    PINNED,
29    ONE_FRAME_ONE_MAP,
30    MEMOBJ_VFS, // see lib/vfs/mmap.c
31    MEMOBJ_FIXED,
32    MEMOBJ_NUMA,
33    MEMOBJ_APPEND,
34};
35
36typedef uint32_t memobj_flags_t;
37typedef uint32_t vs_prot_flags_t;
38
39struct memobj;
40struct vregion;
41struct memobj_funcs {
42    errval_t (*map_region)(struct memobj *memobj, struct vregion *vregion);
43    errval_t (*unmap_region)(struct memobj* memobj, struct vregion* region);
44    errval_t (*protect)(struct memobj* memobj, struct vregion* region,
45                        genvaddr_t offset, size_t range, vs_prot_flags_t flags);
46    errval_t (*pin)(struct memobj* memobj, struct vregion* region,
47                    genvaddr_t offset, size_t range);
48    errval_t (*unpin)(struct memobj* memobj, struct vregion* region,
49                      genvaddr_t offset, size_t range);
50    errval_t (*fill)(struct memobj *memobj, genvaddr_t offset, struct capref frame,
51                     size_t size);
52    errval_t (*fill_foff)(struct memobj *memobj, genvaddr_t offset, struct capref frame,
53                     size_t size, genpaddr_t foffset);
54    errval_t (*unfill)(struct memobj *memobj, genvaddr_t offset,
55                       struct capref *ret_frame, genvaddr_t *ret_offset);
56    errval_t (*pagefault)(struct memobj* memobj, struct vregion* region,
57                          genvaddr_t offset, vm_fault_type_t type);
58    errval_t (*pager_free)(struct memobj* memobj, size_t size,
59                           struct capref *frames, size_t num_frames);
60};
61
62struct vregion_list {
63    struct vregion *region;
64    struct vregion_list *next;
65};
66
67/// Public interface for memobj
68struct memobj {
69    size_t size;              ///< Size of the object
70    memobj_flags_t flags;     ///< Flags for the object. NYI.
71    enum memobj_type type;    ///< Type of the memory object
72    struct memobj_funcs f;    ///< Function pointers
73};
74
75struct memobj_pinned {
76    struct memobj m;          ///< Public interface
77    struct vregion *vregion;  ///< Pointer to the single vregion
78};
79
80struct memobj_one_frame_one_map {
81    struct memobj m;          ///< Public interface
82    struct vregion *vregion;  ///< Pointer to the single vregion
83    struct capref frame;      ///< Frame tracked by the obj
84    genpaddr_t offset;        ///< Offset into the frame
85};
86
87struct memobj_one_frame {
88    struct memobj m;
89    struct vregion_list *vregion_list;    ///< List of vregions mapped into the obj
90    struct capref frame;                  ///< Frame tracked by the obj
91    genpaddr_t offset;                    ///< Offset into the frame
92};
93
94struct memobj_one_frame_lazy {
95    struct memobj m;
96    struct vregion_list *vregion_list;    ///< List of vregions mapped into the obj
97    struct capref frame;                  ///< Frame tracked by the obj
98    size_t chunk_size;                    ///< Amount to map in per pagefault
99};
100
101struct memobj_frame_list {
102    genpaddr_t offset;              ///< Offset into the frame
103    struct capref frame;            ///< Capability of the frame
104    size_t size;                    ///< Size of the frame
105    genpaddr_t pa;                  ///< XXX: physical address of frame
106    genpaddr_t foffset;             ///< Offset into frame
107    struct memobj_frame_list *next;
108};
109
110struct memobj_anon {
111    struct memobj m;
112    struct vregion_list *vregion_list;    ///< List of vregions mapped into the obj
113    struct slab_allocator vregion_slab;       ///< Slab to back the vregion list
114    struct memobj_frame_list *frame_list; ///< List of frames tracked by the obj
115    struct slab_allocator frame_slab;         ///< Slab to back the frame list
116    bool frame_slab_refilling;      ///< True, iff we're currently refilling `frame_slab`
117    bool vregion_slab_refilling;    ///< True, iff we're currently refilling `vregion_slab`
118};
119
120/**
121 * this memobj can be mapped into a single vregion and backed by a fixed number
122 * of equal sized frames
123 */
124struct memobj_fixed {
125    struct memobj    m;          ///< public memobj interface
126    size_t           count;      ///< the number of frames
127    size_t           chunk_size; ///< the size of the frames
128    struct vregion  *vregion;    ///< the associated vregion
129    struct capref   *frames;     ///< the tracked frames
130    size_t          *offsets;    ///< the offset into the tracked frames
131};
132
133/**
134 * this memobj can be mapped into a single vregion and is backed by a s
135 * this memobj can be mapped into a single vregion and backed by a fixed number
136 * of equal sized frames
137 */
138struct memobj_numa {
139    struct memobj    m;          ///< public memobj interface
140    uint32_t         node_count; ///< number of nodes in the machine
141    size_t           stride;     ///< size of the regions to map
142    struct vregion  *vregion;    ///< the associated vregion
143    struct capref   *frames;     ///< the tracked frames
144};
145
146/**
147 * this memobj can be mapped into a single vregion and is backed by a list of
148 * frames.  This memobj only supports appending frames at the end.
149 */
150struct memobj_append {
151    struct memobj   m;
152    struct vregion *vregion;           ///< associated vregion
153    struct capref  *frames;            ///< array of frame caps backing the object
154    genvaddr_t     *offsets;           ///< offsets into object for associated frames
155    gensize_t      *frame_sizes;       ///< sizes of frame caps
156    size_t         *frame_offsets;     ///< offsets into frame caps
157    size_t          frame_count;       ///< number of entries in arrays
158    size_t          first_free_frame;  ///< first free entry in arrays
159    genvaddr_t      already_faulted;   ///< highest offset in object that we already mapped
160    size_t          first_unfaulted;   ///< index of first unfaulted entry in frame array
161};
162
163errval_t memobj_create_pinned(struct memobj_pinned *memobj, size_t size,
164                              memobj_flags_t flags);
165
166errval_t memobj_create_anon(struct memobj_anon *memobj, size_t size,
167                            memobj_flags_t flags);
168errval_t memobj_destroy_anon(struct memobj *memobj, bool delete_caps);
169
170errval_t memobj_create_one_frame(struct memobj_one_frame *memobj, size_t size,
171                                 memobj_flags_t flags);
172errval_t memobj_destroy_one_frame(struct memobj *memobj);
173
174errval_t memobj_create_one_frame_lazy(struct memobj_one_frame_lazy *memobj,
175                                      size_t size, memobj_flags_t flags,
176                                      struct capref frame, size_t chunk_size);
177errval_t memobj_create_one_frame_one_map(struct memobj_one_frame_one_map *memobj,
178                                         size_t size, memobj_flags_t flags);
179
180
181errval_t memobj_create_fixed(struct memobj_fixed *memobj, size_t size,
182                             memobj_flags_t flags, size_t count,
183                             size_t chunk_size);
184
185errval_t memobj_destroy_fixed(struct memobj *memobj);
186
187errval_t memobj_create_numa(struct memobj_numa *numa, size_t size,
188                            memobj_flags_t flags, size_t node_count, size_t stride);
189
190errval_t memobj_destroy_numa(struct memobj *memobj);
191
192errval_t memobj_create_append(struct memobj_append *append,
193                              size_t size,
194                              memobj_flags_t flags);
195errval_t memobj_destroy_append(struct memobj *memobj);
196
197__END_DECLS
198
199#endif // LIBBARRELFISH_MEMOBJ_H
200