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
|
/* ----------------------------------------------------------------------- *
*
* Copyright 2001-2008 H. Peter Anvin - All Rights Reserved
*
* This program 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, Inc., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
/*
* e820func.c
*
* E820 range database manager
*/
#include <stdint.h>
#ifdef TEST
#include <string.h>
#else
#include "memdisk.h" /* For memset() */
#endif
#include "e820.h"
#define MAXRANGES 1024
/* All of memory starts out as one range of "indeterminate" type */
struct e820range ranges[MAXRANGES];
int nranges;
void e820map_init(void)
{
memset(ranges, 0, sizeof(ranges));
nranges = 1;
ranges[1].type = -1U;
}
static void insertrange_at(int where, uint64_t start,
uint32_t type, uint32_t extattr)
{
int i;
for ( i = nranges ; i > where ; i-- )
ranges[i] = ranges[i-1];
ranges[where].start = start;
ranges[where].type = type;
ranges[where].extattr = extattr;
nranges++;
ranges[nranges].start = 0ULL;
ranges[nranges].type = -1U;
ranges[nranges].extattr = 0;
}
void insertrange(uint64_t start, uint64_t len, uint32_t type, uint32_t extattr)
{
uint64_t last;
uint32_t oldtype, oldattr;
int i, j;
/* Remove this to make len == 0 mean all of memory */
if ( len == 0 )
return; /* Nothing to insert */
last = start+len-1; /* May roll over */
i = 0;
oldtype = -2U;
oldattr = 0;
while ( start > ranges[i].start && ranges[i].type != -1U ) {
oldtype = ranges[i].type;
oldattr = ranges[i].extattr;
i++;
}
/* Consider the replacement policy. This current one is "overwrite." */
if ( start < ranges[i].start || ranges[i].type == -1U )
insertrange_at(i++, start, type, extattr);
while ( i == 0 || last > ranges[i].start-1 ) {
oldtype = ranges[i].type;
oldattr = ranges[i].extattr;
ranges[i].type = type;
ranges[i].extattr = extattr;
i++;
}
if ( last < ranges[i].start-1 )
insertrange_at(i, last+1, oldtype, oldattr);
/* Now the map is correct, but quite possibly not optimal. Scan the
map for ranges which are redundant and remove them. */
i = j = 1;
oldtype = ranges[0].type;
oldattr = ranges[0].extattr;
while ( i < nranges ) {
if ( ranges[i].type == oldtype && ranges[i].extattr == oldattr ) {
i++;
} else {
oldtype = ranges[i].type;
oldattr = ranges[i].extattr;
if ( i != j )
ranges[j] = ranges[i];
i++; j++;
}
}
if ( i != j ) {
ranges[j] = ranges[i]; /* Termination sentinel copy */
nranges -= (i-j);
}
}
|