17
17
*********************************************************/
22
* Wrappers for FreeBSD system functions required by "vmmemctl".
23
* This allows customers to build their own vmmemctl driver for
24
* custom FreeBSD kernels without the need for source code.
22
* Wrappers for FreeBSD system functions required by "vmmemctl".
28
26
* Compile-Time Options
31
#define OS_DISABLE_UNLOAD (0)
29
#define OS_DISABLE_UNLOAD 0
109
104
static void vmmemctl_init_sysctl(void);
110
105
static void vmmemctl_deinit_sysctl(void);
116
void *os_kmalloc_nosleep(unsigned int size)
118
return(malloc(size, M_VMMEMCTL, M_NOWAIT));
121
void os_kfree(void *obj, unsigned int size)
123
free(obj, M_VMMEMCTL);
126
void os_bzero(void *b, unsigned int len)
131
void os_memcpy(void *dest, const void *src, unsigned int size)
109
*-----------------------------------------------------------------------------
113
* Allocates kernel memory.
116
* On success: Pointer to allocated memory
122
*-----------------------------------------------------------------------------
126
OS_Malloc(size_t size) // IN
128
return malloc(size, M_VMMEMCTL, M_NOWAIT);
133
*-----------------------------------------------------------------------------
137
* Free allocated kernel memory.
145
*-----------------------------------------------------------------------------
149
OS_Free(void *ptr, // IN
152
free(ptr, M_VMMEMCTL);
157
*-----------------------------------------------------------------------------
161
* Fill a memory location with 0s.
169
*-----------------------------------------------------------------------------
173
OS_MemZero(void *ptr, // OUT
181
*-----------------------------------------------------------------------------
185
* Copy a memory portion into another location.
193
*-----------------------------------------------------------------------------
197
OS_MemCopy(void *dest, // OUT
198
const void *src, // IN
133
201
memcpy(dest, src, size);
151
int os_sprintf(char *str, const char *format, ...)
221
*-----------------------------------------------------------------------------
225
* Print a string into a bounded memory location.
228
* Number of character printed including trailing \0.
233
*-----------------------------------------------------------------------------
237
OS_Snprintf(char *buf, // OUT
239
const char *format, // IN
154
245
va_start(args, format);
155
return(vsprintf(str, format, args));
159
* System-Dependent Operations
162
char *os_identity(void)
168
* Predict the maximum achievable balloon size.
170
* Currently we just return the total memory pages.
172
unsigned int os_predict_max_balloon_pages(void)
174
return(cnt.v_page_count);
177
unsigned long os_addr_to_ppn(unsigned long addr)
179
return (((vm_page_t)addr)->phys_addr) >> PAGE_SHIFT;
182
static void os_pmap_alloc(os_pmap *p)
246
result = vsnprintf(buf, size, format, args);
254
*-----------------------------------------------------------------------------
258
* Returns an identifier for the guest OS family.
266
*-----------------------------------------------------------------------------
272
return BALLOON_GUEST_BSD;
277
*-----------------------------------------------------------------------------
279
* OS_ReservedPageGetLimit --
281
* Predict the maximum achievable balloon size.
284
* Total memory pages.
289
*-----------------------------------------------------------------------------
293
OS_ReservedPageGetLimit(void)
295
return cnt.v_page_count;
300
*-----------------------------------------------------------------------------
302
* OS_ReservedPageGetPPN --
304
* Convert a page handle (of a physical page previously reserved with
305
* OS_ReservedPageAlloc()) to a ppn.
313
*-----------------------------------------------------------------------------
317
OS_ReservedPageGetPPN(PageHandle handle) // IN: A valid page handle
319
return (((vm_page_t)handle)->phys_addr) >> PAGE_SHIFT;
324
os_pmap_alloc(os_pmap *p) // IN
184
326
/* number of pages (div. 8) */
185
327
p->size = (cnt.v_page_count + 7) / 8;
188
330
* expand to nearest word boundary
189
331
* XXX: bitmap can be greater than total number of pages in system
194
336
p->bitmap = (unsigned long *)kmem_alloc(kernel_map, p->size);
197
static void os_pmap_free(os_pmap *p)
341
os_pmap_free(os_pmap *p) // IN
199
343
kmem_free(kernel_map, (vm_offset_t)p->bitmap, p->size);
201
345
p->bitmap = NULL;
204
static void os_pmap_init(os_pmap *p)
350
os_pmap_init(os_pmap *p) // IN
206
352
/* alloc bitmap for pages in system */
208
354
if (!p->bitmap) {
210
356
p->bitmap = NULL;
248
396
return (vm_pindex_t)-1;
251
static void os_pmap_putindex(os_pmap *p, vm_pindex_t pindex)
401
os_pmap_putindex(os_pmap *p, // IN
402
vm_pindex_t pindex) // IN
254
p->bitmap[pindex / (8*sizeof(unsigned long))] &=
405
p->bitmap[pindex / (8*sizeof(unsigned long))] &=
255
406
~(1<<(pindex % (8*sizeof(unsigned long))));
258
static void os_kmem_free(vm_page_t page)
411
os_kmem_free(vm_page_t page) // IN
260
413
os_state *state = &global_state;
261
414
os_pmap *pmap = &state->pmap;
304
static void os_balloonobject_delete(void)
461
os_balloonobject_delete(void)
306
463
vm_object_deallocate(global_state.vmobject);
309
static void os_balloonobject_create(void)
468
os_balloonobject_create(void)
311
global_state.vmobject = vm_object_allocate(OBJT_DEFAULT,
470
global_state.vmobject = vm_object_allocate(OBJT_DEFAULT,
312
471
OFF_TO_IDX(VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS));
315
unsigned long os_alloc_reserved_page(int can_sleep)
317
return (unsigned long)os_kmem_alloc(can_sleep);
320
void os_free_reserved_page(unsigned long page)
322
os_kmem_free((vm_page_t)page);
325
static void os_timer_internal(void *data)
476
*-----------------------------------------------------------------------------
478
* OS_ReservedPageAlloc --
480
* Reserve a physical page for the exclusive use of this driver.
483
* On success: A valid page handle that can be passed to OS_ReservedPageGetPPN()
484
* or OS_ReservedPageFree().
485
* On failure: PAGE_HANDLE_INVALID
490
*-----------------------------------------------------------------------------
494
OS_ReservedPageAlloc(int canSleep) // IN
498
page = os_kmem_alloc(canSleep);
500
return PAGE_HANDLE_INVALID;
503
return (PageHandle)page;
508
*-----------------------------------------------------------------------------
510
* OS_ReservedPageFree --
512
* Unreserve a physical page previously reserved with OS_ReservedPageAlloc().
520
*-----------------------------------------------------------------------------
524
OS_ReservedPageFree(PageHandle handle) // IN: A valid page handle
526
os_kmem_free((vm_page_t)handle);
531
os_timer_internal(void *data) // IN
327
533
os_timer *t = (os_timer *) data;
336
void os_timer_init(os_timer_handler handler, void *data, int period)
544
*-----------------------------------------------------------------------------
548
* Setup the timer callback function, then start it.
551
* Always TRUE, cannot fail.
556
*-----------------------------------------------------------------------------
560
OS_TimerStart(OSTimerHandler *handler, // IN
561
void *clientData) // IN
338
563
os_timer *t = &global_state.timer;
565
/* setup the timer structure */
340
566
callout_handle_init(&t->callout_handle);
341
567
t->handler = handler;
347
void os_timer_start(void)
349
os_timer *t = &global_state.timer;
568
t->data = clientData;
351
571
/* clear termination flag */
354
574
/* scheduler timer handler */
355
575
t->callout_handle = timeout(os_timer_internal, t, t->period);
358
void os_timer_stop(void)
582
*-----------------------------------------------------------------------------
594
*-----------------------------------------------------------------------------
360
600
os_timer *t = &global_state.timer;
366
606
untimeout(os_timer_internal, t, t->callout_handle);
369
unsigned int os_timer_hz(void)
611
*-----------------------------------------------------------------------------
615
* Yield the CPU, if needed.
623
*-----------------------------------------------------------------------------
376
629
/* Do nothing. */
379
void os_init(const char *name,
380
const char *name_verbose,
381
os_status_handler handler)
634
*-----------------------------------------------------------------------------
638
* Called at driver startup, initializes the balloon state and structures.
647
*-----------------------------------------------------------------------------
651
OS_Init(const char *name, // IN
652
const char *nameVerbose, // IN
653
OSStatusHandler *handler) // IN
383
655
os_state *state = &global_state;
384
656
os_pmap *pmap = &state->pmap;
398
670
/* initialize status state */
399
671
state->status.handler = handler;
400
672
state->status.name = name;
401
state->status.name_verbose = name_verbose;
673
state->status.name_verbose = nameVerbose;
403
675
os_pmap_init(pmap);
404
676
os_balloonobject_create();
408
680
/* log device load */
409
681
printf("%s initialized\n", state->status.name_verbose);
412
void os_cleanup(void)
687
*-----------------------------------------------------------------------------
691
* Called when the driver is terminating, cleanup initialized structures.
699
*-----------------------------------------------------------------------------
414
705
os_state *state = &global_state;
415
706
os_pmap *pmap = &state->pmap;
424
715
printf("%s unloaded\n", status->name_verbose);
428
720
* Module Load/Unload Operations
431
extern int init_module(void);
432
extern void cleanup_module(void);
434
static int vmmemctl_load(module_t mod, int cmd, void *arg)
725
vmmemctl_load(module_t mod, // IN: Unused
727
void *arg) // IN: Unused
440
(void) init_module();
733
if (Balloon_ModuleInit() != BALLOON_SUCCESS) {
444
739
if (OS_DISABLE_UNLOAD) {
445
/* prevent moudle unload */
740
/* prevent module unload */
743
Balloon_ModuleCleanup();