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.


be723992a6cd26b27fac052108ab3c13af000279
[palacios.git] / palacios / src / devices / char_stream.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) 2010, Peter Dinda <pdinda@northwestern.edu>
11  * Copyright (c) 2010, The V3VEE Project <http://www.v3vee.org> 
12  * All rights reserved.
13  *
14  * Author: Peter Dinda <pdinda@northwestern.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.h>
21 #include <palacios/vmm_stream.h>
22 #include <palacios/vmm_dev_mgr.h>
23 #include <palacios/vmm_sprintf.h>
24 #include <palacios/vmm_host_events.h>
25 #include <palacios/vmm_lock.h>
26 #include <palacios/vmm_string.h>
27
28
29 struct stream_state {
30     v3_stream_t stream;
31
32     struct v3_dev_char_ops char_ops;
33
34     void * push_fn_arg;
35 };
36
37
38 static int serial_event_handler(struct v3_vm_info * vm, 
39                                 struct v3_serial_event * evt, 
40                                 void * private_data) {
41     struct stream_state * state = (struct stream_state *)private_data;
42
43     if (state->char_ops.push != NULL){
44         state->char_ops.push(vm, evt->data, evt->len, state->push_fn_arg);
45     }
46
47     return 0;
48 }
49
50 static int stream_write(uint8_t * buf, uint64_t length, void * private_data) {
51     struct stream_state * state = (struct stream_state *)private_data;
52     
53     return v3_stream_write(state->stream, buf, length);
54 }
55
56 static int stream_free(struct vm_device * dev) {
57     struct stream_state * state = (struct stream_state *)(dev->private_data);
58
59     v3_stream_close(state->stream);
60
61     // detach host event
62
63     V3_Free(state);
64
65     return 0;
66 }
67
68
69 static struct v3_device_ops dev_ops = {
70     .free = stream_free,
71 };
72
73 static int stream_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
74     char * dev_id = v3_cfg_val(cfg, "ID");
75     char * stream_name = v3_cfg_val(cfg, "name");
76     struct stream_state * state = NULL;
77
78     v3_cfg_tree_t * frontend_cfg = v3_cfg_subtree(cfg, "frontend");
79
80     state = (struct stream_state *)V3_Malloc(sizeof(struct stream_state));
81
82     if (state == NULL) {
83         PrintError("Could not allocate stream backend device\n");
84         return -1;
85     }
86
87     memset(state, 0, sizeof(struct stream_state));
88
89     struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
90
91     if (dev == NULL) {
92         PrintError("Could not allocate device %s\n", dev_id);
93         V3_Free(state);
94         return -1;
95     }
96
97
98
99     state->stream = v3_stream_open(vm, stream_name);
100
101     if (state->stream == NULL) {
102         PrintError("Could not open stream %s\n", stream_name);
103         v3_remove_device(dev);
104         return -1;
105     }
106
107     state->char_ops.write = stream_write;
108
109     if (v3_dev_connect_char(vm, v3_cfg_val(frontend_cfg, "tag"), 
110                             &(state->char_ops), frontend_cfg, 
111                             state, &(state->push_fn_arg)) == -1) {
112         PrintError("Could not connect %s to frontend %s\n", 
113                    dev_id, v3_cfg_val(frontend_cfg, "tag"));
114         v3_remove_device(dev);
115         return -1;
116     }
117
118     v3_hook_host_event(vm, HOST_SERIAL_EVT, V3_HOST_EVENT_HANDLER(serial_event_handler), state);
119
120     return 0;
121 }
122
123 device_register("CHAR_STREAM", stream_init);