iPXE
memmap_settings.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <string.h>
27 #include <errno.h>
28 #include <byteswap.h>
29 #include <ipxe/init.h>
30 #include <ipxe/settings.h>
31 #include <ipxe/memmap.h>
32 
33 /** @file
34  *
35  * Memory map settings
36  *
37  * Memory map settings are numerically encoded as:
38  *
39  * Bits 31-24 Number of regions, minus one
40  * Bits 23-16 Starting region
41  * Bits 15-11 Unused
42  * Bit 10 Ignore non-existent regions (rather than generating an error)
43  * Bit 9 Include length
44  * Bit 8 Include start address
45  * Bits 7-6 Unused
46  * Bits 5-0 Scale factor (i.e. right shift count)
47  */
48 
49 /**
50  * Construct memory map setting tag
51  *
52  * @v start Starting region
53  * @v count Number of regions
54  * @v include_start Include start address
55  * @v include_length Include length
56  * @v ignore Ignore non-existent regions
57  * @v scale Scale factor
58  * @ret tag Setting tag
59  */
60 #define MEMMAP_TAG( start, count, include_start, include_length, \
61  ignore, scale ) \
62  ( ( (start) << 16 ) | ( ( (count) - 1 ) << 24 ) | \
63  ( (ignore) << 10 ) | ( (include_length) << 9 ) | \
64  ( (include_start) << 8 ) | (scale) )
65 
66 /**
67  * Extract number of regions from setting tag
68  *
69  * @v tag Setting tag
70  * @ret count Number of regions
71  */
72 #define MEMMAP_COUNT( tag ) ( ( ( (tag) >> 24 ) & 0xff ) + 1 )
73 
74 /**
75  * Extract starting region from setting tag
76  *
77  * @v tag Setting tag
78  * @ret start Starting region
79  */
80 #define MEMMAP_START( tag ) ( ( (tag) >> 16 ) & 0xff )
81 
82 /**
83  * Extract ignore flag from setting tag
84  *
85  * @v tag Setting tag
86  * @ret ignore Ignore non-existent regions
87  */
88 #define MEMMAP_IGNORE_NONEXISTENT( tag ) ( (tag) & 0x00000400UL )
89 
90 /**
91  * Extract length inclusion flag from setting tag
92  *
93  * @v tag Setting tag
94  * @ret include_length Include length
95  */
96 #define MEMMAP_INCLUDE_LENGTH( tag ) ( (tag) & 0x00000200UL )
97 
98 /**
99  * Extract start address inclusion flag from setting tag
100  *
101  * @v tag Setting tag
102  * @ret include_start Include start address
103  */
104 #define MEMMAP_INCLUDE_START( tag ) ( (tag) & 0x00000100UL )
105 
106 /**
107  * Extract scale factor from setting tag
108  *
109  * @v tag Setting tag
110  * @v scale Scale factor
111  */
112 #define MEMMAP_SCALE( tag ) ( (tag) & 0x3f )
113 
114 /** Memory map settings scope */
116 
117 /**
118  * Check applicability of memory map setting
119  *
120  * @v settings Settings block
121  * @v setting Setting
122  * @ret applies Setting applies within this settings block
123  */
125  const struct setting *setting ) {
126 
127  return ( setting->scope == &memmap_settings_scope );
128 }
129 
130 /**
131  * Fetch value of memory map setting
132  *
133  * @v settings Settings block
134  * @v setting Setting to fetch
135  * @v data Buffer to fill with setting data
136  * @v len Length of buffer
137  * @ret len Length of setting data, or negative error
138  */
140  struct setting *setting,
141  void *data, size_t len ) {
142  struct memmap_region region;
143  uint64_t result = 0;
144  unsigned int index = 0;
145  unsigned int start;
146  unsigned int count;
147  unsigned int scale;
148  int include_start;
149  int include_length;
150  int ignore_nonexistent;
151 
152  /* Parse settings tag */
153  start = MEMMAP_START ( setting->tag );
154  count = MEMMAP_COUNT ( setting->tag );
155  scale = MEMMAP_SCALE ( setting->tag );
156  include_start = MEMMAP_INCLUDE_START ( setting->tag );
157  include_length = MEMMAP_INCLUDE_LENGTH ( setting->tag );
158  ignore_nonexistent = MEMMAP_IGNORE_NONEXISTENT ( setting->tag );
159  DBGC ( settings, "MEMMAP start %d count %d %s%s%s%s scale %d\n",
160  start, count, ( include_start ? "start" : "" ),
161  ( ( include_start && include_length ) ? "+" : "" ),
162  ( include_length ? "length" : "" ),
163  ( ignore_nonexistent ? " ignore" : "" ), scale );
164 
165  /* Extract results from memory map */
166  for_each_memmap ( &region, 0 ) {
167 
168  /* Skip non-memory regions */
169  if ( ! ( region.flags & MEMMAP_FL_MEMORY ) )
170  continue;
171 
172  /* Ignore unwanted regions */
173  if ( index++ < start )
174  continue;
175 
176  /* Extract results from this region */
177  if ( include_start ) {
178  result += region.min;
179  DBGC ( settings, "MEMMAP %d start %#08llx\n", index,
180  ( ( unsigned long long ) region.min ) );
181  }
182  if ( include_length ) {
183  result += memmap_size ( &region );
184  DBGC ( settings, "MEMMAP %d length %#08llx\n", index,
185  ( ( unsigned long long )
186  memmap_size ( &region ) ) );
187  }
188 
189  /* Stop when we have accumulated sufficient regions */
190  if ( --count == 0 )
191  break;
192  }
193 
194  /* Check for nonexistent regions */
195  if ( count && ( ! ignore_nonexistent ) ) {
196  DBGC ( settings, "MEMMAP regions %d-%d do not exist\n",
197  index, ( index + count - 1 ) );
198  return -ENOENT;
199  }
200 
201  /* Scale result */
202  result >>= scale;
203 
204  /* Return result */
205  result = cpu_to_be64 ( result );
206  if ( len > sizeof ( result ) )
207  len = sizeof ( result );
208  memcpy ( data, &result, len );
209 
210  /* Set type if not already specified */
211  if ( ! setting->type )
212  setting->type = &setting_type_hexraw;
213 
214  return sizeof ( result );
215 }
216 
217 /** Memory map settings operations */
220  .fetch = memmap_settings_fetch,
221 };
222 
223 /** Memory map settings */
224 static struct settings memmap_settings = {
225  .refcnt = NULL,
226  .siblings = LIST_HEAD_INIT ( memmap_settings.siblings ),
227  .children = LIST_HEAD_INIT ( memmap_settings.children ),
229  .default_scope = &memmap_settings_scope,
230 };
231 
232 /** Initialise memory map settings */
233 static void memmap_settings_init ( void ) {
234  int rc;
235 
237  "memmap" ) ) != 0 ) {
238  DBG ( "MEMMAP could not register settings: %s\n",
239  strerror ( rc ) );
240  return;
241  }
242 }
243 
244 /** Memory map settings initialiser */
245 struct init_fn memmap_settings_init_fn __init_fn ( INIT_NORMAL ) = {
246  .name = "memmap",
247  .initialise = memmap_settings_init,
248 };
249 
250 /** Memory map predefined settings */
251 const struct setting memsize_setting __setting ( SETTING_MISC, memsize ) = {
252  .name = "memsize",
253  .description = "Memory size (in MB)",
254  .tag = MEMMAP_TAG ( 0, 0x100, 0, 1, 1, 20 ),
255  .type = &setting_type_int32,
256  .scope = &memmap_settings_scope,
257 };
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const struct setting memsize_setting __setting(SETTING_MISC, memsize)
Memory map predefined settings.
#define MEMMAP_COUNT(tag)
Extract number of regions from setting tag.
Error codes.
#define DBGC(...)
Definition: compiler.h:505
#define MEMMAP_INCLUDE_LENGTH(tag)
Extract length inclusion flag from setting tag.
long index
Definition: bigint.h:62
#define ENOENT
No such file or directory.
Definition: errno.h:514
unsigned long long uint64_t
Definition: stdint.h:13
#define SETTING_MISC
Miscellaneous settings.
Definition: settings.h:80
#define MEMMAP_SCALE(tag)
Extract scale factor from setting tag.
struct init_fn memmap_settings_init_fn __init_fn(INIT_NORMAL)
Memory map settings initialiser.
static int memmap_settings_applies(struct settings *settings __unused, const struct setting *setting)
Check applicability of memory map setting.
static struct settings_operations memmap_settings_operations
Memory map settings operations.
const char * name
Name.
Definition: settings.h:28
uint32_t start
Starting offset.
Definition: netvsc.h:12
uint64_t tag
Setting tag, if applicable.
Definition: settings.h:43
#define MEMMAP_TAG(start, count, include_start, include_length, ignore, scale)
Construct memory map setting tag.
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:31
void * memcpy(void *dest, const void *src, size_t len) __nonnull
An initialisation function.
Definition: init.h:14
static int memmap_settings_fetch(struct settings *settings, struct setting *setting, void *data, size_t len)
Fetch value of memory map setting.
#define for_each_memmap(region, hide)
Iterate over memory regions.
Definition: memmap.h:183
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
ring len
Length.
Definition: dwmac.h:231
const char * name
Definition: init.h:15
static unsigned int count
Number of entries.
Definition: dwmac.h:225
const struct setting_type * type
Setting type.
Definition: settings.h:36
static void memmap_settings_init(void)
Initialise memory map settings.
Configuration settings.
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static const struct settings_scope memmap_settings_scope
Memory map settings scope.
#define MEMMAP_START(tag)
Extract starting region from setting tag.
unsigned int flags
Region flags.
Definition: memmap.h:54
Settings block operations.
Definition: settings.h:85
A settings block.
Definition: settings.h:132
A setting scope.
Definition: settings.h:176
struct list_head siblings
Sibling settings blocks.
Definition: settings.h:140
A setting.
Definition: settings.h:23
uint16_t result
Definition: hyperv.h:33
static struct settings memmap_settings
Memory map settings.
static uint64_t memmap_size(const struct memmap_region *region)
Get remaining size of memory region (from the described address upwards)
Definition: memmap.h:98
#define MEMMAP_FL_MEMORY
Contains memory.
Definition: memmap.h:59
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define cpu_to_be64(value)
Definition: byteswap.h:111
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint64_t min
Minimum address in region.
Definition: memmap.h:50
int(* applies)(struct settings *settings, const struct setting *setting)
Check applicability of setting.
Definition: settings.h:98
struct list_head children
Child settings blocks.
Definition: settings.h:142
int register_settings(struct settings *settings, struct settings *parent, const char *name)
Register settings block.
Definition: settings.c:475
A memory region descriptor.
Definition: memmap.h:48
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
const struct settings_scope * scope
Setting scope (or NULL)
Definition: settings.h:49
#define MEMMAP_IGNORE_NONEXISTENT(tag)
Extract ignore flag from setting tag.
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
System memory map.
String functions.
struct refcnt * refcnt
Reference counter.
Definition: settings.h:134
#define MEMMAP_INCLUDE_START(tag)
Extract start address inclusion flag from setting tag.