summaryrefslogtreecommitdiff
path: root/libhurd-mm/as-compute-gbits.h
blob: 4236ee70445a53c8f3865cce91d1f32f9ef2c5b5 (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
/* as-compute-gbits.h - Guard bit calculator.
   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/>.  */

#include <viengoos/folio.h>

struct as_guard_cappage
{
  int gbits;
  int cappage_width;
};

/* Given UNTRANSLATED bits and a maximum guard length of GBITS, return
   the optimal guard length.  */
static struct as_guard_cappage
as_compute_gbits_cappage (int untranslated_bits, int to_translate,
			  int gbits)
{
  /* Our strategy is as follows: we want to avoid 1) having to move
     page tables around, and 2) small cappages.  We know that folios
     will be mapped such that their data pages are visible in the data
     address space of the process, i.e., at /VG_ADDR_BITS-7-12.  Thus, we
     try to ensure that we have 7-bit cappages at /VG_ADDR_BITS-7-12 and
     then 8-bit cappage at /VG_ADDR_BITS-7-12-i*8, i > 0, i.e., /44, /36,
     etc.  */

  assertx (untranslated_bits > 0 && to_translate > 0 && gbits >= 0
	   && untranslated_bits >= to_translate
	   && untranslated_bits >= gbits
	   && to_translate >= gbits,
	   "untranslated_bits: %d, to_translate: %d, gbits: %d",
	   untranslated_bits, to_translate, gbits);

  if (untranslated_bits <= PAGESIZE_LOG2)
    ;

  /* There could be less than PAGESIZE_LOG2 untranslated bits.  Place
     a cappage at /VG_ADDR_BITS-PAGESIZE_LOG2.

      UNTRANSLATED_BITS
     |--------------------|
     |-------|
      GBITS
           |--------------|
            PAGESIZE_LOG2

           ^
  */
  else if (untranslated_bits - gbits <= PAGESIZE_LOG2)
    gbits = untranslated_bits - PAGESIZE_LOG2;

  /* There could be less than VG_FOLIO_OBJECTS_LOG2 + PAGESIZE_LOG2
     untranslated bits.  Place a cappage at
     /VG_ADDR_BITS-VG_FOLIO_OBJECTS_LOG2-PAGESIZE_LOG2.

      UNTRANSLATED_BITS
     |--------------------|
     |-------|
      GBITS
           |------|-------|
           |       PAGESIZE_LOG2
           `VG_FOLIO_OBJECTS_LOG2

	   ^ 
  */
  else if (untranslated_bits - gbits <= VG_FOLIO_OBJECTS_LOG2 + PAGESIZE_LOG2)
    gbits = untranslated_bits - VG_FOLIO_OBJECTS_LOG2 - PAGESIZE_LOG2;

  /* 
           UNTRANSLATED_BITS
     |-----------------------------|

     |----------|-------|----|-----|
     |          |       |     PAGESIZE_LOG2
     |          |       `VG_FOLIO_OBJECTS_LOG2
     `GBITS     `REMAINDER

     Shrink GBITS such that REMAINDER becomes a multiple of
     VG_CAPPAGE_SLOTS_LOG2.
   */
  else
    {
      int remainder = untranslated_bits - gbits
	- VG_FOLIO_OBJECTS_LOG2 - PAGESIZE_LOG2;

      /* Amount to remove from GBITS such that REMAINDER + TO_REMOVE is a
	 multiple of VG_CAPPAGE_SLOTS_LOG2.  */
      int to_remove = VG_CAPPAGE_SLOTS_LOG2 - (remainder % VG_CAPPAGE_SLOTS_LOG2);

      if (to_remove < gbits)
	gbits -= to_remove;
      else
	gbits = 0;
    }

  assert (gbits >= 0);

  struct as_guard_cappage gc;
  if (untranslated_bits - gbits == VG_FOLIO_OBJECTS_LOG2 + PAGESIZE_LOG2)
    gc.cappage_width = VG_FOLIO_OBJECTS_LOG2;
  else
    gc.cappage_width = VG_CAPPAGE_SLOTS_LOG2;

  if (gbits + gc.cappage_width > to_translate)
    gc.cappage_width = to_translate - gbits;

  gc.gbits = gbits;

  return gc;
}