summaryrefslogtreecommitdiff
path: root/hurd/addr-trans.h
blob: 931368423b63c03bc33c74f19bb1db5b88f5ed8f (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
/* addr-trans.h - Address translation functions.
   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
   Written by Neal H. Walfield <neal@gnu.org>.

   This file is part of the GNU Hurd.

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

   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
   Lesser General Public License for more details.

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

#ifndef _HURD_ADDR_TRANS_H
#define _HURD_ADDR_TRANS_H

#include <l4/types.h>
#include <hurd/stddef.h>

/* Capabilities have two primary functions: they designate objects and
   they participate in address translation.  This structure controls
   how the page table walker translates bits when passing through this
   capability.  */

#define CAP_ADDR_TRANS_GUARD_SUBPAGE_BITS 22
#define CAP_ADDR_TRANS_SUBPAGES_BITS 4
#define CAP_ADDR_TRANS_GDEPTH_BITS 6

struct cap_addr_trans
{
  union
  {
    struct
    {
      /* The value of the guard and the subpage to use.

      A capability page is partitioned into 2^SUBPAGES_LOG2 subpages.
      This value determines the number of subpage index bits and
      maximum number of guard bits.  The number of subpage index bits
      is SUBPAGES_LOG2 and the number of guard bits is the remainder
      (the guard lies in the upper bits; the subpage in the lower).

      If SUBPAGES_LOG2 is 0, there is a single subpage (covering the
      entire page).  This implies that there are no subpage bits (the
      only valid offset is 0) and 21 possible guard bits.  If
      SUBPAGES_LOG2 is 0, there are 256 subpages, 8 subpage bits and a
      maximum of 21-8=15 guard bits.  */
      l4_uint32_t guard_subpage: CAP_ADDR_TRANS_GUARD_SUBPAGE_BITS;
      /* The log2 of the subpages.  The size of a subpage is thus 2^(8 -
	 SUBPAGES_LOG2).  Values of SUBPAGES_LOG2 other than 0 are only
	 allowed for cap pages.  */
      l4_uint32_t subpages_log2: CAP_ADDR_TRANS_SUBPAGES_BITS;
      /* Number of significant guard bits.  The value of the GUARD is zero
	 extended if GDEPTH is greater than the number of available guard
	 bits.  */
      l4_uint32_t gdepth: CAP_ADDR_TRANS_GDEPTH_BITS;
    };
    l4_uint32_t raw;
  };
};

#define CAP_ADDR_TRANS_INIT { { .raw = 0 } }
#define CAP_ADDR_TRANS_VOID (struct cap_addr_trans) { { .raw = 0 } }

/* The log2 number of subpages.  */
#define CAP_ADDR_TRANS_SUBPAGES_LOG2(cap_addr_trans_) \
  ((cap_addr_trans_).subpages_log2)

/* The number of subpages.  */
#define CAP_ADDR_TRANS_SUBPAGES(cap_addr_trans_) \
  (1 << CAP_ADDR_TRANS_SUBPAGES_LOG2((cap_addr_trans_)))

/* The designated subpage.  */
#define CAP_ADDR_TRANS_SUBPAGE(cap_addr_trans_) \
  ((cap_addr_trans_).guard_subpage \
   & (CAP_ADDR_TRANS_SUBPAGES ((cap_addr_trans_)) - 1))

/* The log2 of the size of the named subpage (in capability
   units).  */
#define CAP_ADDR_TRANS_SUBPAGE_SIZE_LOG2(cap_addr_trans_) \
  (8 - (cap_addr_trans_).subpages_log2)

/* The number of caps addressed by this capability.  */
#define CAP_ADDR_TRANS_SUBPAGE_SIZE(cap_addr_trans_) \
  (1 << CAP_ADDR_TRANS_SUBPAGE_SIZE_LOG2 ((cap_addr_trans_)))

/* The offset in capability units (with respect to the start of the
   capability page) of the first capability in the designated
   sub-page.  */
#define CAP_ADDR_TRANS_SUBPAGE_OFFSET(cap_addr_trans_) \
  (CAP_ADDR_TRANS_SUBPAGE ((cap_addr_trans_)) \
   * CAP_ADDR_TRANS_SUBPAGE_SIZE ((cap_addr_trans_)))

/* The number of guard bits.  */
#define CAP_ADDR_TRANS_GUARD_BITS(cap_addr_trans_) ((cap_addr_trans_).gdepth)

/* The value of the guard.  */
#define CAP_ADDR_TRANS_GUARD(cap_addr_trans_) \
  ((l4_uint64_t) ((cap_addr_trans_).guard_subpage \
		  >> (cap_addr_trans_).subpages_log2))

#define CATSGST_(test_, format, args...) \
  if (! (test_)) \
    { \
      r_ = false; \
      debug (1, format, ##args); \
    }

/* Set CAP_ADDR_TRANS_P_'s guard and the subpage.  Returns true on success
   (parameters valid), false otherwise.  */
#define CAP_ADDR_TRANS_SET_GUARD_SUBPAGE(cap_addr_trans_p_, guard_, gdepth_, \
					 subpage_, subpages_) \
  ({ bool r_ = true; \
     /* There must be at least 1 subpage.  */ \
     CATSGST_ (((subpages_) > 0), \
	       "subpages_ (%d) must be at least 1\n", (subpages_)); \
     CATSGST_ (((subpages_) & ((subpages_) - 1)) == 0, \
               "SUBPAGES_ (%d) must be a power of 2\n", (subpages_)); \
     int subpages_log2_ = l4_msb ((subpages_)) - 1; \
     CATSGST_ (subpages_log2_ <= 8, \
               "maximum subpages is 256 (%d)\n", (subpages_)); \
     CATSGST_ (0 <= (subpage_) && (subpage_) < (subpages_), \
               "subpage (%d) must be between 0 and SUBPAGES_ (%d) - 1\n", \
               (subpage_), (subpages_)); \
     \
     /* The number of required guard bits.  */ \
     int gbits_ = l4_msb64 ((guard_)); \
     CATSGST_ (gbits_ <= (gdepth_), \
               "Significant guard bits (%d) must be less than depth (%d)\n", \
               gbits_, (gdepth_)); \
     CATSGST_ (gbits_ + subpages_log2_ <= CAP_ADDR_TRANS_GUARD_SUBPAGE_BITS, \
               "Significant guard bits (%d) plus subpage bits (%d) > %d\n", \
               gbits_, subpages_log2_, CAP_ADDR_TRANS_GUARD_SUBPAGE_BITS); \
     \
     if (r_) \
       { \
         (cap_addr_trans_p_)->subpages_log2 = subpages_log2_; \
         (cap_addr_trans_p_)->gdepth = (gdepth_); \
         (cap_addr_trans_p_)->guard_subpage \
           = ((guard_) << subpages_log2_) | (subpage_); \
       } \
     r_; \
  })

/* Set *CAP_ADDR_TRANS_P_'s guard.  Returns true on success (parameters
   valid), false otherwise.  */
#define CAP_ADDR_TRANS_SET_GUARD(cap_addr_trans_p_, guard_, gdepth_) \
  ({ int subpage_ = CAP_ADDR_TRANS_SUBPAGE (*(cap_addr_trans_p_)); \
     int subpages_ = CAP_ADDR_TRANS_SUBPAGES (*(cap_addr_trans_p_)); \
     CAP_ADDR_TRANS_SET_GUARD_SUBPAGE ((cap_addr_trans_p_), \
				       (guard_), (gdepth_), \
				       (subpage_), (subpages_)); \
  })

/* Set *CAP_ADDR_TRANS_P_'s subpage.  Returns true on success (parameters
   valid), false otherwise.  */
#define CAP_ADDR_TRANS_SET_SUBPAGE(cap_addr_trans_p_, subpage_, subpages_) \
  ({ int gdepth_ = CAP_ADDR_TRANS_GUARD_BITS (*(cap_addr_trans_p_)); \
     int guard_ = CAP_ADDR_TRANS_GUARD (*(cap_addr_trans_p_)); \
     CAP_ADDR_TRANS_SET_GUARD_SUBPAGE ((cap_addr_trans_p_), \
				       (guard_), (gdepth_), \
				       (subpage_), (subpages_)); \
  })

/* Returns whether the capability address CAP_ADDR_TRANS is well-formed.  */
#define CAP_ADDR_TRANS_VALID(cap_addr_trans) \
  ({ bool r_ = true; \
     CATSGST_ (CAP_ADDR_TRANS_GUARD_BITS (cap_addr_trans) <= L4_WORDSIZE, \
	       "Invalid guard depth (%d)", \
	       CAP_ADDR_TRANS_GUARD_BITS (cap_addr_trans)); \
     CATSGST_ (CAP_ADDR_TRANS_SUBPAGES_LOG2 (cap_addr_trans) <= 8, \
               "Invalid number of subpages (%d)", \
               CAP_ADDR_TRANS_SUBPAGES (cap_addr_trans)); \
     CATSGST_ (l4_msb (CAP_ADDR_TRANS_GUARD (cap_addr_trans)) \
	       <= CAP_ADDR_TRANS_GUARD_BITS (cap_addr_trans), \
               "Significant guard bits (%d) exceeds guard depth (%d)", \
               l4_msb (CAP_ADDR_TRANS_GUARD (cap_addr_trans)), \
	       CAP_ADDR_TRANS_GUARD_BITS (cap_addr_trans)); \
     r_; \
  })

#endif