| 1 |
/****************************************************************************** |
| 2 |
* tools/xenpaging/xenpaging.h |
| 3 |
* |
| 4 |
* Xen domain paging. |
| 5 |
* |
| 6 |
* Copyright (c) 2009 Citrix Systems, Inc. (Patrick Colp) |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; If not, see <http://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
#ifndef __XEN_PAGING2_H__ |
| 24 |
#define __XEN_PAGING2_H__ |
| 25 |
|
| 26 |
#include <malloc.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <fcntl.h> |
| 31 |
#include <sys/mman.h> |
| 32 |
|
| 33 |
#include <xenevtchn.h> |
| 34 |
#define XC_WANT_COMPAT_MAP_FOREIGN_API |
| 35 |
#include <xenctrl.h> |
| 36 |
// #include <xc_private.h> |
| 37 |
#include <xen/event_channel.h> |
| 38 |
#include <xen/vm_event.h> |
| 39 |
|
| 40 |
#define XENPAGING_PAGEIN_QUEUE_SIZE 64 |
| 41 |
|
| 42 |
struct vm_event { |
| 43 |
domid_t domain_id; |
| 44 |
xenevtchn_handle *xce_handle; |
| 45 |
int port; |
| 46 |
vm_event_back_ring_t back_ring; |
| 47 |
uint32_t evtchn_port; |
| 48 |
void *ring_page; |
| 49 |
}; |
| 50 |
|
| 51 |
struct xenpaging { |
| 52 |
xc_interface *xc_handle; |
| 53 |
xentoollog_logger *logger; |
| 54 |
struct xs_handle *xs_handle; |
| 55 |
|
| 56 |
unsigned long *bitmap; |
| 57 |
|
| 58 |
unsigned long *slot_to_gfn; |
| 59 |
int *gfn_to_slot; |
| 60 |
|
| 61 |
void *paging_buffer; |
| 62 |
|
| 63 |
struct vm_event vm_event; |
| 64 |
int fd; |
| 65 |
/* number of pages for which data structures were allocated */ |
| 66 |
int max_pages; |
| 67 |
int num_paged_out; |
| 68 |
int target_tot_pages; |
| 69 |
int policy_mru_size; |
| 70 |
int use_poll_timeout; |
| 71 |
int debug; |
| 72 |
int stack_count; |
| 73 |
int *free_slot_stack; |
| 74 |
unsigned long pagein_queue[XENPAGING_PAGEIN_QUEUE_SIZE]; |
| 75 |
}; |
| 76 |
|
| 77 |
#define DPRINTF(msg, args...) xtl_log(paging->logger, XTL_DETAIL, 0, \ |
| 78 |
"paging", msg, ## args) |
| 79 |
#define ERROR(msg, args...) xtl_log(paging->logger, XTL_ERROR, -1, \ |
| 80 |
"paging", msg, ## args) |
| 81 |
#define PERROR(msg, args...) xtl_log(paging->logger, XTL_ERROR, -1, \ |
| 82 |
"paging", msg "(%d = %s)", ## args, \ |
| 83 |
errno, strerror(errno)) |
| 84 |
|
| 85 |
extern void create_page_in_thread(struct xenpaging *paging); |
| 86 |
extern void page_in_trigger(void); |
| 87 |
|
| 88 |
#define BITS_PER_LONG (sizeof(unsigned long) * 8) |
| 89 |
#define ORDER_LONG (sizeof(unsigned long) == 4 ? 5 : 6) |
| 90 |
|
| 91 |
#define BITMAP_ENTRY(_nr,_bmap) ((_bmap))[(_nr) / 8] |
| 92 |
#define BITMAP_SHIFT(_nr) ((_nr) % 8) |
| 93 |
|
| 94 |
static inline int bitmap_size(int nr_bits) |
| 95 |
{ |
| 96 |
return (nr_bits + 7) / 8; |
| 97 |
} |
| 98 |
|
| 99 |
static inline void *bitmap_alloc(int nr_bits) |
| 100 |
{ |
| 101 |
return calloc(1, bitmap_size(nr_bits)); |
| 102 |
} |
| 103 |
|
| 104 |
static inline void bitmap_clear(void *addr, int nr_bits) |
| 105 |
{ |
| 106 |
memset(addr, 0, bitmap_size(nr_bits)); |
| 107 |
} |
| 108 |
|
| 109 |
static inline int test_bit(int nr, const void *_addr) |
| 110 |
{ |
| 111 |
const char *addr = _addr; |
| 112 |
return (BITMAP_ENTRY(nr, addr) >> BITMAP_SHIFT(nr)) & 1; |
| 113 |
} |
| 114 |
|
| 115 |
static inline void clear_bit(int nr, void *_addr) |
| 116 |
{ |
| 117 |
char *addr = _addr; |
| 118 |
BITMAP_ENTRY(nr, addr) &= ~(1UL << BITMAP_SHIFT(nr)); |
| 119 |
} |
| 120 |
|
| 121 |
static inline void set_bit(int nr, void *_addr) |
| 122 |
{ |
| 123 |
char *addr = _addr; |
| 124 |
BITMAP_ENTRY(nr, addr) |= (1UL << BITMAP_SHIFT(nr)); |
| 125 |
} |
| 126 |
|
| 127 |
static inline int test_and_clear_bit(int nr, void *addr) |
| 128 |
{ |
| 129 |
int oldbit = test_bit(nr, addr); |
| 130 |
clear_bit(nr, addr); |
| 131 |
return oldbit; |
| 132 |
} |
| 133 |
|
| 134 |
static inline int test_and_set_bit(int nr, void *addr) |
| 135 |
{ |
| 136 |
int oldbit = test_bit(nr, addr); |
| 137 |
set_bit(nr, addr); |
| 138 |
return oldbit; |
| 139 |
} |
| 140 |
|
| 141 |
#endif // __XEN_PAGING_H__ |
| 142 |
|
| 143 |
|
| 144 |
/* |
| 145 |
* Local variables: |
| 146 |
* mode: C |
| 147 |
* c-file-style: "BSD" |
| 148 |
* c-basic-offset: 4 |
| 149 |
* tab-width: 4 |
| 150 |
* indent-tabs-mode: nil |
| 151 |
* End: |
| 152 |
*/ |