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.


Support for new Palacios image file format (Version 1)
[palacios.git] / linux_usr / v3_mem_free.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7
8 #include "v3_ctrl.h"
9
10 int main(void) 
11 {
12     FILE * fp;
13     char * filepath = "/proc/v3vee/v3-mem";
14     char line[255];
15     unsigned long base_addr = 0;
16     unsigned long long num_pages = 0;
17     unsigned long amount_allocated = 0;
18     unsigned long long block_size = 0;
19     int i = 0;
20     int v3_fd;
21     
22     fp = fopen(filepath, "r");
23     if(fp == NULL) {
24         fprintf(stderr, "Could not open %s\n", filepath);
25         return -1;
26     }
27     memset(line, 0, 255);
28     fgets(line, 255, fp);
29     base_addr = strtoul(line, NULL, 16);
30     base_addr = base_addr / (1024*1024);
31     memset(line, 0, 255);
32     fgets(line, 255, fp);
33     num_pages = strtoull(line, NULL, 10);
34     amount_allocated = (num_pages*4096)/(1024*1024);
35     
36     fclose(fp);
37     
38     /* now get the block size */
39     fp = fopen("/sys/devices/system/memory/block_size_bytes", "r");
40     if(fp == NULL) {
41         fprintf(stderr, "Cannot lookup bytes per block size\n");
42         return -1;
43     }
44     memset(line, 0, 255);
45     fgets(line, 255, fp);
46     block_size = strtoull(line, NULL, 16);
47     block_size = block_size / (1024*1024); //convert to MB
48     fclose(fp);
49     
50     /* turn base_addr into the region number */
51     base_addr = base_addr / block_size;
52     
53     for(i=0;i< (amount_allocated / block_size); i++) {
54         char path[50];
55         
56         memset(path, 0, 50);
57         sprintf(path,"/sys/devices/system/memory/memory%d/state", 
58                 base_addr);
59         fp = fopen(path, "w+");
60         if(fp == NULL) {
61             fprintf(stderr, "Could not open %s\n", path);
62             return -1;
63         }
64         printf("Sending \"online\" to memory%d\n", base_addr);
65         fprintf(fp, "online");
66         fclose(fp);
67         base_addr++;
68     }
69     
70     v3_fd = open(v3_dev, O_RDONLY);
71     if (v3_fd == -1) {
72         printf("Error opening V3Vee control device\n");
73         return -1;
74     }
75
76     ioctl(v3_fd, V3_RESET_MEMORY, NULL);
77     close(v3_fd);
78
79     return 0;
80 }