-
Notifications
You must be signed in to change notification settings - Fork 31
/
memory.h
81 lines (61 loc) · 2.24 KB
/
memory.h
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
#ifndef MEMORY_H
#define MEMORY_H
#include "types.h"
struct queue_head;
/* memory types */
typedef enum {
ADDR_UNDEF,
ADDR_OUTSIDE,
ADDR_INSIDE,
} address_type_t;
/* this is our simple heap for executable and writable pages */
typedef struct heap_s {
struct heap_s *next;
word elem_size;
word allocated;
word num_elem;
byte *first_elem;
} heap_t;
typedef struct dyn_mem_s {
struct dyn_mem_s *next;
struct dyn_mem_s *head;
word size;
byte data[1];
} dyn_mem_t;
/* copy memory from safely from any type of memory to any type of memory (optional - from different processes) */
int safe_memory_copy(void *dst, const void *src, word len, int dst_hint, int src_hint, word dst_pid, word src_pid);
/* allocate executable and writable memory */
void *memory_alloc_exec(word size);
/* free an executable buffer */
void memory_free_exec(void *mem);
/* start memory - makes sure all the structures are initialized */
void memory_start(void);
/* stop memory, and delete any buffer that wasn't deleted */
void memory_stop(void);
/* initialize a dynamic memory head */
void memory_dyn_init(dyn_mem_t *head);
/* free an entire dynamic memory struct */
void memory_dyn_clean(dyn_mem_t *head);
/* allocate a dynamic memory */
void *memory_alloc_dyn(dyn_mem_t *head, word size);
/* free a dynamic memory */
int memory_free_dyn(dyn_mem_t *head, void *ptr);
/* checks if a pointer is a dynamic memory */
dyn_mem_t *get_dyn_mem(dyn_mem_t *head, void *ptr);
/* transfer a dynamic memory from one head to another */
int transfer_dyn_mem(dyn_mem_t *head, dyn_mem_t *dyn);
/* callback for the queue struct */
void dyn_free_callback(void *data);
/* get a buffer from user */
int recv_data_from_other(struct queue_head *queue, dyn_mem_t *head, dyn_mem_t **dyn, int nonblock);
/* send a buffer to user */
int send_data_to_other(struct queue_head *queue, dyn_mem_t *dyn, int nonblock);
/* check memory permissions */
int memory_check_addr_perm(const byte *addr, word *size, int write, byte *read_only);
/* check if a memory is executable */
int memory_check_addr_exec(const byte *addr);
/* map an outside memory to an inside memory */
int memory_map(const byte *addr, word *size, void **map, byte **new_addr, int write);
/* unmap an outside memory */
void memory_unmap(byte *addr);
#endif