summaryrefslogtreecommitdiff
path: root/viengoos/memory.h
blob: dd6af262278762dc44af13908c80ba6a37af08f3 (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
/* memory.h - Basic memory management interface.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3 of the
   License, or (at your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see
   <http://www.gnu.org/licenses/>.  */

#ifndef RM_MEMORY_H
#define RM_MEMORY_H

#include <stdint.h>
#include <stdbool.h>

#define PAGES_FMT "%"PRIdPTR" %s"
#define PAGES_PRINTF(pages)			\
  ({						\
    int64_t p_ = (pages) * PAGESIZE;		\
    int neg = p_ < 0;				\
    if (neg)					\
      p_ = -p_;					\
						\
    if (p_ > 10 * 1024 * 1024)			\
      p_ /= 1024 * 1024;			\
    else if (p_ > 10 * 1024)			\
      p_ /= 1024;				\
						\
    if (neg)					\
      p_ = -p_;					\
    p_;						\
  }),						\
  ({						\
    int64_t p_ = (pages) * PAGESIZE;		\
    if (p_ < 0)					\
      p_ = -p_;					\
    char *s_ = "bytes";				\
    if (p_ > 10 * 1024 * 1024)			\
      s_ = "mb";				\
    else if (p_ > 10 * 1024)			\
      s_ = "kb";				\
    s_;						\
  })


/* Forward.  */
struct activity;

enum memory_reservation
  {
    /* Our binary, never freed.  */
    memory_reservation_self = 1,
    /* Memory used during the initialization.  */
    memory_reservation_init,
    /* Memory used by boot modules.  */
    memory_reservation_system_executable,
    /* Memory used by boot modules.  */
    memory_reservation_modules,
  };

/* Total number of frames.  */
extern uint32_t memory_total;

/* Address of the first byte of the first frame.  */
extern uintptr_t first_frame;
/* Address of the first byte of the last frame.  */
extern uintptr_t last_frame;

/* Reserve the memory starting at byte START and ending at byte END
   with the reservation RESERVATION.  The memory is added to the free
   pool if and when the reservation expires.  Returns true on success.
   Otherwise false, if the reservation could not be completed because
   of an existing reservation.  */
extern bool memory_reserve (uintptr_t start, uintptr_t end,
			    enum memory_reservation reservation);

/* Print the reserved regions.  */
extern void memory_reserve_dump (void);

/* If there is reserved memory occuring on or after byte START and on
   or before byte END, return true and the first byte of the first
   contiguous region in *START_RESERVATION and the last byte in
   *END_RESERVATION.  Otherwise, false.  */
extern bool memory_is_reserved (uintptr_t start, uintptr_t end,
				uintptr_t *start_reservation,
				uintptr_t *end_reservation);

/* Cause the reservation RESERVATION to expire.  Add all memory with
   this reservation to the free pool.  */
extern void memory_reservation_clear (enum memory_reservation reservation);

/* Allocate a page of memory on behalf of activity ACTIVITY.  Returns
   NULL if there is no memory available.  */
extern uintptr_t memory_frame_allocate (struct activity *activity);

/* Return the frame starting at address ADDR to the free pool.  */
extern void memory_frame_free (uintptr_t addr);

/* Add the memory starting at byte START and continuing until byte END
   to the free pool.  START must name the first byte in a physical
   frame and END the last.  START and END must be kernel virtual
   addresses.  */
extern void memory_add (uintptr_t start, uintptr_t end);

#endif