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.


Avoid strict-aliasing related issues when compiling with optimization
[palacios.git] / linux_usr / v3_os_debug.c
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <sys/select.h>
7 #include <malloc.h>
8
9 #include "v3_user_host_dev.h"
10
11 void usage()
12 {
13     fprintf(stderr,"v3_user_host_dev_example /dev/v3-vm0 user:mydev busywait|select\n");
14 }
15
16
17 int do_work(struct palacios_host_dev_host_request_response *req, 
18             struct palacios_host_dev_host_request_response **resp)
19 {
20     uint64_t datasize;
21
22     switch (req->type) {
23     case PALACIOS_HOST_DEV_HOST_REQUEST_WRITE_IO: {
24       if (req->port==0xc0c0) {
25         uint64_t i;
26         uint64_t numchars;
27         numchars = req->data_len - sizeof(struct palacios_host_dev_host_request_response);
28         for (i=0;i<numchars;i++) { 
29           putchar(req->data[i]);
30         }
31         *resp = malloc(sizeof(struct palacios_host_dev_host_request_response));
32         if (!*resp) { 
33             printf("Cannot allocate response\n");
34             return -1;
35         }
36         **resp=*req;
37         (*resp)->len = (*resp)->data_len = sizeof(struct palacios_host_dev_host_request_response);
38         (*resp)->op_len = numchars;
39       } else {
40         printf("Huh?  Unknown port %d\n",req->port);
41       }
42     }
43       break;
44
45       default:
46         printf("Huh?  Unknown request %d\n", req->type);
47     }
48     
49     
50     return 0;
51 }
52
53 int main(int argc, char *argv[])
54 {
55     int devfd;
56     int mode=0;
57     char *vm, *url;
58
59     if (argc!=4) { 
60         usage();
61         exit(-1);
62     }
63     
64     vm=argv[1];
65     url=argv[2];
66     mode = argv[3][0]=='s';
67
68     fprintf(stderr,"Attempting to rendezvous with host device %s on vm %s\n", url, vm);
69     
70     if ((devfd = v3_user_host_dev_rendezvous(vm,url))<0) { 
71         perror("failed to rendezvous");
72         exit(-1);
73     }
74     
75     fprintf(stderr,"Rendezvous succeeded, I will now operate in %s mode\n", mode==0 ? "busywait" : "select");
76     
77     if (mode==0) { 
78         //busywait
79
80         struct palacios_host_dev_host_request_response *req;
81         struct palacios_host_dev_host_request_response *resp;
82         uint64_t datasize;
83
84         while (1) { 
85             while (!(v3_user_host_dev_have_request(devfd))) { 
86             }
87             v3_user_host_dev_pull_request(devfd, &req);
88
89             do_work(req, &resp);
90             
91             v3_user_host_dev_push_response(devfd, resp);
92
93             free(resp);
94             free(req);
95         }
96     } else {
97
98         struct palacios_host_dev_host_request_response *req;
99         struct palacios_host_dev_host_request_response *resp;
100         uint64_t datasize;
101         fd_set   readset;
102         int rc;
103
104         // select-based operation so that you can wait for multiple things
105         
106         while (1) { 
107             FD_ZERO(&readset);
108             FD_SET(devfd,&readset);
109
110             rc = select(devfd+1, &readset, 0, 0, 0);  // pick whatever you want to select on, just include devfd
111
112             if (rc>0) { 
113                 if (FD_ISSET(devfd,&readset)) { 
114                     // a request is read for us!
115                     v3_user_host_dev_pull_request(devfd, &req);
116
117                     do_work(req, &resp);
118                     
119                     v3_user_host_dev_push_response(devfd, resp);
120                     
121                     free(resp);
122                     free(req);
123                 }
124             }
125         }
126     }
127
128     v3_user_host_dev_depart(devfd);
129
130     return 0;
131                     
132 }