Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


ff7a7a76021762c59f2961a85393792225f32f53
[palacios.git] / palacios / src / palacios / vmm_bitmap.c
1 /*
2  * This file is part of the Palacios Virtual Machine Monitor developed
3  * by the V3VEE Project with funding from the United States National 
4  * Science Foundation and the Department of Energy.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
10  * Copyright (c) 2011, Jack Lange <jacklange@cs.pitt.edu> 
11  * Copyright (c) 2011, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Jack Lange <jacklange@cs.pitt.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #include <palacios/vmm_bitmap.h>
21 #include <palacios/vmm.h>
22
23
24 int v3_bitmap_init(struct v3_bitmap * bitmap, int num_bits) {
25     int num_bytes = (num_bits / 8) + ((num_bits % 8) > 0);
26
27     v3_lock_init(&(bitmap->lock));
28     bitmap->num_bits = num_bits;
29     bitmap->bits = V3_Malloc(num_bytes);
30
31
32     if (bitmap->bits == NULL) {
33         PrintError("Could not allocate bitmap of %d bits\n", num_bits);
34         return -1;
35     }
36     
37     memset(bitmap->bits, 0, num_bytes);
38
39     return 0;
40 }
41
42
43 void v3_bitmap_deinit(struct v3_bitmap * bitmap) {
44     V3_Free(bitmap->bits);
45 }
46
47
48 int v3_bitmap_reset(struct v3_bitmap * bitmap) {
49     int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
50
51     memset(bitmap->bits, 0, num_bytes);
52
53     return 0;
54 }
55
56 int v3_bitmap_set(struct v3_bitmap * bitmap, int index) {
57     int major = index / 8;
58     int minor = index % 8;
59     int old_val = 0;
60     uint32_t flags = 0;
61
62     if (index > (bitmap->num_bits - 1)) {
63         PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
64                    index, bitmap->num_bits);
65         return -1;
66     }
67
68
69     flags = v3_lock_irqsave(bitmap->lock);
70
71     old_val = (bitmap->bits[major] & (0x1 << minor));
72     bitmap->bits[major] |= (0x1 << minor);
73
74     v3_unlock_irqrestore(bitmap->lock, flags);
75
76     return old_val;
77 }
78
79
80 int v3_bitmap_clear(struct v3_bitmap * bitmap, int index) {
81     int major = index / 8;
82     int minor = index % 8;
83     int old_val = 0;
84     uint32_t flags = 0;
85
86     if (index > (bitmap->num_bits - 1)) {
87         PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
88                    index, bitmap->num_bits);
89         return -1;
90     }
91
92     flags = v3_lock_irqsave(bitmap->lock);
93
94     old_val = (bitmap->bits[major] & (0x1 << minor));
95     bitmap->bits[major] &= ~(0x1 << minor);
96
97     v3_unlock_irqrestore(bitmap->lock, flags);
98
99     return old_val;
100 }
101
102 int v3_bitmap_check(struct v3_bitmap * bitmap, int index) {
103     int major = index / 8;
104     int minor = index % 8;
105
106     if (index > (bitmap->num_bits - 1)) {
107         PrintError("Index out of bitmap range: (pos = %d) (num_bits = %d)\n", 
108                    index, bitmap->num_bits);
109         return -1;
110     }
111
112     return ((bitmap->bits[major] & (0x1 << minor)) != 0);
113 }
114
115
116 int v3_bitmap_count(struct v3_bitmap * bitmap) {
117
118     int cnt = 0;
119     int i;
120     uint8_t x;
121     uint8_t * bytes = bitmap->bits;
122     int num_bytes = (bitmap->num_bits / 8) + ((bitmap->num_bits % 8) > 0);
123
124     for (i=0; i < num_bytes; i++) {
125         x = bytes[i];
126         while (x) { 
127             cnt += (x & 0x1);
128             x>>=1;
129         }
130     }     
131     
132     return cnt;
133 }
134
135 int v3_bitmap_copy(struct v3_bitmap * dst, struct v3_bitmap * src) {
136     
137     if (src->num_bits != dst->num_bits) {
138         PrintError("src and dst must be the same size.\n");
139         return -1;    
140     }
141     
142     int num_bytes = (src->num_bits / 8) + ((src->num_bits % 8)!=0);
143     
144     memcpy(dst->bits,src->bits,num_bytes);
145     
146     return 0;
147 }