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.


Resource control extensions to host OS interface
[palacios.git] / palacios / src / devices / telnet_cons.c
index 00415c5..be40ba4 100644 (file)
 #include <palacios/vmm_host_events.h>
 #include <palacios/vmm_lock.h>
 #include <palacios/vmm_string.h>
-#include <palacios/vmm_socket.h>
+#include <interfaces/vmm_socket.h>
 
 #include <devices/console.h>
-#if 0
-#include <devices/telnet_cons.h>
-#endif
+
 
 #define NUM_ROWS 25
 #define NUM_COLS 80
@@ -50,8 +48,8 @@
 
 
 struct cons_state {
-    int server_fd;
-    int client_fd;
+    v3_sock_t server_fd;
+    v3_sock_t client_fd;
 
     uint16_t port;
 
@@ -59,6 +57,8 @@ struct cons_state {
 
     v3_lock_t cons_lock;
 
+    struct v3_vm_info * vm;
+
     struct vm_device * frontend_dev;
 };
 
@@ -105,7 +105,7 @@ static const struct key_code ascii_to_key_code[] = {             // ASCII Value
 
 
 
-static int deliver_scan_code(struct vm_device * dev, struct key_code * key) {
+static int deliver_scan_code(struct cons_state * state, struct key_code * key) {
     struct v3_keyboard_event key_event;
     struct v3_keyboard_event key_shift;
     uint_t cap = key->capital;
@@ -113,30 +113,30 @@ static int deliver_scan_code(struct vm_device * dev, struct key_code * key) {
     key_event.status = 0;
     key_event.scan_code = (uint8_t)key->scan_code;
 
-    PrintDebug("Scan code: 0x%x\n", key_event.scan_code);
+    PrintDebug(VM_NONE, VCORE_NONE, "Scan code: 0x%x\n", key_event.scan_code);
 
 
     if (cap) {
        key_shift.status = 0;
        key_shift.scan_code = (uint8_t)0x2A;
 
-       if (v3_deliver_keyboard_event(dev->vm, &key_shift) == -1) {
-           PrintError("Video: Error delivering key event\n");
+       if (v3_deliver_keyboard_event(state->vm, &key_shift) == -1) {
+           PrintError(VM_NONE, VCORE_NONE, "Video: Error delivering key event\n");
            return -1;
        }
     }
 
     // Press
-    if (v3_deliver_keyboard_event(dev->vm, &key_event) == -1) {
-       PrintError("Video: Error delivering key event\n");
+    if (v3_deliver_keyboard_event(state->vm, &key_event) == -1) {
+       PrintError(VM_NONE, VCORE_NONE, "Video: Error delivering key event\n");
        return -1;
     }
 
     // Release
     key_event.scan_code = key_event.scan_code | 0x80;
   
-    if (v3_deliver_keyboard_event(dev->vm, &key_event) == -1) {
-       PrintError("Video: Error delivering key event\n");
+    if (v3_deliver_keyboard_event(state->vm, &key_event) == -1) {
+       PrintError(VM_NONE, VCORE_NONE, "Video: Error delivering key event\n");
        return -1;
     }
 
@@ -144,32 +144,32 @@ static int deliver_scan_code(struct vm_device * dev, struct key_code * key) {
     if (cap) {
         key_shift.scan_code = 0x2A | 0x80;
 
-       if (v3_deliver_keyboard_event(dev->vm, &key_shift) == -1) {
-           PrintError("Video: Error delivering key event\n");
+       if (v3_deliver_keyboard_event(state->vm, &key_shift) == -1) {
+           PrintError(VM_NONE, VCORE_NONE, "Video: Error delivering key event\n");
            return -1;
        }
     }
 
-    PrintDebug("Finished with Key delivery\n");
+    PrintDebug(VM_NONE, VCORE_NONE, "Finished with Key delivery\n");
     return 0;
 }
 
 
 
 
-static int recv_all(int socket, char * buf, int length) {
+static int recv_all(v3_sock_t socket, char * buf, int length) {
     int bytes_read = 0;
     
-    PrintDebug("Reading %d bytes\n", length - bytes_read);
+    PrintDebug(VM_NONE, VCORE_NONE, "Reading %d bytes\n", length - bytes_read);
     while (bytes_read < length) {
-        int tmp_bytes = V3_Recv(socket, buf + bytes_read, length - bytes_read);
-        PrintDebug("Received %d bytes\n", tmp_bytes);
+        int tmp_bytes = v3_socket_recv(socket, buf + bytes_read, length - bytes_read);
+        PrintDebug(VM_NONE, VCORE_NONE, "Received %d bytes\n", tmp_bytes);
 
         if (tmp_bytes == 0) {
-            PrintError("Connection Closed unexpectedly\n");
+            PrintError(VM_NONE, VCORE_NONE, "Connection Closed unexpectedly\n");
             return 0;
         } else if (tmp_bytes == -1) {
-           PrintError("Socket Error in for V3_RECV\n");
+           PrintError(VM_NONE, VCORE_NONE, "Socket Error in for V3_RECV\n");
            return -1;
        }
 
@@ -180,13 +180,13 @@ static int recv_all(int socket, char * buf, int length) {
 }
 
 
-static int send_all(const int sock, const char * buf, const int len){
+static int send_all(const v3_sock_t sock, const char * buf, const int len){
     int bytes_left = len;
 
     while (bytes_left != 0) {
        int written = 0;
 
-       if ((written = V3_Send(sock, buf + (len - bytes_left), bytes_left)) == -1) {
+       if ((written = v3_socket_send(sock, buf + (len - bytes_left), bytes_left)) == -1) {
            return -1;
        }
 
@@ -214,8 +214,7 @@ static const uint8_t bg_color_map[] = {
     } while (0)
 
 
-static int send_update(struct vm_device * dev, uint8_t  x, uint8_t  y, uint8_t attrib, uint8_t val) {
-    struct cons_state * state = (struct cons_state *)dev->private_data;
+static int send_update(struct cons_state * state, uint8_t  x, uint8_t  y, uint8_t attrib, uint8_t val) {
     uint8_t fg_color = fg_color_map[(attrib & 0x0f) % 16];
     uint8_t bg_color = bg_color_map[(attrib & 0xf0) % 16];
     uint8_t buf[32];
@@ -259,7 +258,7 @@ static int send_update(struct vm_device * dev, uint8_t  x, uint8_t  y, uint8_t a
     buf[i++] = 'H';
     buf[i++] = val;
 
-    PrintDebug("printing value '%c'\n", val);
+    PrintDebug(VM_NONE, VCORE_NONE, "printing value '%c'\n", val);
 
     if (state->connected) {
        uint64_t start, end;
@@ -268,7 +267,7 @@ static int send_update(struct vm_device * dev, uint8_t  x, uint8_t  y, uint8_t a
        ret =  send_all(state->client_fd, buf, 32);
        rdtscll(end);
 
-       PrintDebug("Sendall latency=%d cycles\n", (uint32_t)(end - start));
+       PrintDebug(VM_NONE, VCORE_NONE, "Sendall latency=%d cycles\n", (uint32_t)(end - start));
     }
 
     return ret;
@@ -277,8 +276,7 @@ static int send_update(struct vm_device * dev, uint8_t  x, uint8_t  y, uint8_t a
 
 
 static int cursor_update(uint_t x, uint_t y, void * private_data) {
-    struct vm_device * dev = (struct vm_device *)private_data;
-    struct cons_state * state = (struct cons_state *)dev->private_data;
+    struct cons_state * state = (struct cons_state *)private_data;
     uint8_t buf[16];
     int ret = 0;
     addr_t irq_state = 0;
@@ -307,8 +305,7 @@ static int cursor_update(uint_t x, uint_t y, void * private_data) {
 
 
 static int screen_update(uint_t x, uint_t y, uint_t length, void * private_data) {
-    struct vm_device * dev = (struct vm_device *)private_data;
-    struct cons_state * state = (struct cons_state *)dev->private_data;
+    struct cons_state * state = (struct cons_state *)private_data;
     uint_t offset = (x * BYTES_PER_COL) + (y * BYTES_PER_ROW);
     uint8_t fb_buf[length];
     int i = 0;
@@ -337,8 +334,8 @@ static int screen_update(uint_t x, uint_t y, uint_t length, void * private_data)
 
        irq_state = v3_lock_irqsave(state->cons_lock);
 
-       if (send_update(dev, cur_x, cur_y, col[1], col[0]) == -1) {
-           PrintError("Could not send attribute to telnet session\n");
+       if (send_update(state, cur_x, cur_y, col[1], col[0]) == -1) {
+           PrintError(VM_NONE, VCORE_NONE, "Could not send attribute to telnet session\n");
            ret = -1;
            break;
        }
@@ -357,8 +354,7 @@ static int screen_update(uint_t x, uint_t y, uint_t length, void * private_data)
 }
 
 static int scroll(int rows, void * private_data) {
-    struct vm_device * dev = (struct vm_device *)private_data;
-    struct cons_state * state = (struct cons_state *)dev->private_data;
+    struct cons_state * state = (struct cons_state *)private_data;
     addr_t irq_state = 0;
     int ret = 0;
 
@@ -372,7 +368,7 @@ static int scroll(int rows, void * private_data) {
 
            if (state->connected) {
                if (send_all(state->client_fd, message, sizeof(message)) == -1) {
-                   PrintError("Could not send scroll command\n");
+                   PrintError(VM_NONE, VCORE_NONE, "Could not send scroll command\n");
                    ret = -1;
                    break;
                }
@@ -394,29 +390,35 @@ static struct v3_console_ops cons_ops = {
     .update_screen = screen_update, 
     .update_cursor = cursor_update,
     .scroll = scroll,
+    .set_text_resolution = NULL,
 };
 
+static int cons_free(struct cons_state * state) {
+
+    // kill thread... ?
+
+    v3_lock_deinit(&(state->cons_lock));
+
+    V3_Free(state);
+    return 0;
+}
+
 
 static struct v3_device_ops dev_ops = {
-    .free = NULL,
-    .reset = NULL,
-    .start = NULL,
-    .stop = NULL,
+    .free = (int (*)(void *))cons_free,
 };
 
 
 
-static int key_handler(struct vm_device * dev, uint8_t ascii) {
-    struct cons_state * state = (struct cons_state *)dev->private_data;
-
-    PrintDebug("Character recieved: 0x%x\n", ascii);
+static int key_handler( struct cons_state * state, uint8_t ascii) {
+    PrintDebug(VM_NONE, VCORE_NONE, "Character recieved: 0x%x\n", ascii);
 
     // printable
     if (ascii < 0x80) {
        const struct key_code * key = &(ascii_to_key_code[ascii]);
 
-       if (deliver_scan_code(dev, (struct key_code *)key) == -1) {
-           PrintError("Could not deliver scan code to vm\n");
+       if (deliver_scan_code(state, (struct key_code *)key) == -1) {
+           PrintError(VM_NONE, VCORE_NONE, "Could not deliver scan code to vm\n");
            return -1;
        }
 
@@ -428,16 +430,16 @@ static int key_handler(struct vm_device * dev, uint8_t ascii) {
        int recv = recv_all(state->client_fd, esc_seq, 2);
 
        if (recv == -1) {
-           PrintError("Video: Error getting key from network\n");
+           PrintError(VM_NONE, VCORE_NONE, "Video: Error getting key from network\n");
            return -1;
        } else if (recv == 0) {
-           PrintDebug("Video: Client Disconnected\n");
+           PrintDebug(VM_NONE, VCORE_NONE, "Video: Client Disconnected\n");
            return -1;
        }
 
 
        if (esc_seq[0] != '[') {
-           PrintDebug("Ignoring non handled escape sequence (codes = %d %d)\n", 
+           PrintDebug(VM_NONE, VCORE_NONE, "Ignoring non handled escape sequence (codes = %d %d)\n", 
                       esc_seq[0], esc_seq[1]);
            return 0;
        }
@@ -445,19 +447,19 @@ static int key_handler(struct vm_device * dev, uint8_t ascii) {
 
        if (esc_seq[1] == 'A') {                // UP ARROW
            struct key_code up = { 0x48, 0 };
-           deliver_scan_code(dev, &up);
+           deliver_scan_code(state, &up);
        } else if (esc_seq[1] == 'B') {         // DOWN ARROW
            struct key_code down = { 0x50, 0 };
-           deliver_scan_code(dev, &down);
+           deliver_scan_code(state, &down);
        } else if (esc_seq[1] == 'C') {         // RIGHT ARROW
            struct key_code right = { 0x4D, 0 };
-           deliver_scan_code(dev, &right);
+           deliver_scan_code(state, &right);
        } else if (esc_seq[1] == 'D') {         // LEFT ARROW
            struct key_code left = { 0x4B, 0 };
-           deliver_scan_code(dev, &left);
+           deliver_scan_code(state, &left);
        }
     } else {
-       PrintError("Invalid character received from network (%c) (code=%d)\n",
+       PrintError(VM_NONE, VCORE_NONE, "Invalid character received from network (%c) (code=%d)\n",
                   ascii, ascii);
        //      return 0;
     }
@@ -466,20 +468,19 @@ static int key_handler(struct vm_device * dev, uint8_t ascii) {
 }
 
 static int cons_server(void * arg) {
-    struct vm_device * dev = (struct vm_device *)arg;
-    struct cons_state * state = (struct cons_state *)dev->private_data;
+    struct cons_state * state = (struct cons_state *)arg;
     
-    state->server_fd = V3_Create_TCP_Socket();
+    state->server_fd = v3_create_tcp_socket(state->vm);
 
 
-    PrintDebug("Video: Socket File Descriptor: %d\n", state->server_fd);
+    PrintDebug(VM_NONE, VCORE_NONE, "Video: Socket File Descriptor: 0x%p\n", state->server_fd);
 
-    if (V3_Bind_Socket(state->server_fd, state->port) == -1) {
-       PrintError("Video: Failed to bind to socket %d\n", state->port);
+    if (v3_socket_bind(state->server_fd, state->port) == -1) {
+       PrintError(VM_NONE, VCORE_NONE, "Video: Failed to bind to socket %d\n", state->port);
     }
 
-    if (V3_Listen_Socket(state->server_fd, 8) == -1) {
-       PrintError("Video: Failed to listen with socket %d\n", state->server_fd);
+    if (v3_socket_listen(state->server_fd, 8) == -1) {
+       PrintError(VM_NONE, VCORE_NONE, "Video: Failed to listen with socket 0x%p\n", state->server_fd);
     }
 
     while (1) {
@@ -488,35 +489,35 @@ static int cons_server(void * arg) {
        uint8_t ascii_code = 0;
        int recv = 0;
 
-       if ((state->client_fd = V3_Accept_Socket(state->server_fd, &client_ip,  &client_port)) == -1) {
-           PrintError("Video: Failed to accept connection on port %d\n", client_port);
+       if ((state->client_fd = v3_socket_accept(state->server_fd, &client_ip,  &client_port)) == 0) {
+           PrintError(VM_NONE, VCORE_NONE, "Video: Failed to accept connection on port %d\n", client_port);
        }
-       PrintDebug("Accepted Telnet Console connection\n");
+       PrintDebug(VM_NONE, VCORE_NONE, "Accepted Telnet Console connection\n");
        state->connected = 1;
 
-       screen_update(0, 0, SCREEN_SIZE, dev);
+       screen_update(0, 0, SCREEN_SIZE, state);
 
        while (1) {
            recv = recv_all(state->client_fd, &ascii_code, sizeof(ascii_code));
 
-           PrintDebug("Telnet console Received %d bytes\n", recv);
+           PrintDebug(VM_NONE, VCORE_NONE, "Telnet console Received %d bytes\n", recv);
 
            if (recv == -1) {
-               PrintError("Video: Error getting key from network\n");
+               PrintError(VM_NONE, VCORE_NONE, "Video: Error getting key from network\n");
                break;
            } else if (recv == 0) {
-               PrintDebug("Video: Client Disconnected\n");
+               PrintDebug(VM_NONE, VCORE_NONE, "Video: Client Disconnected\n");
                break;
            }
 
-           if (key_handler(dev, ascii_code) == -1) {
-               PrintError("Error in key handler\n");
+           if (key_handler(state, ascii_code) == -1) {
+               PrintError(VM_NONE, VCORE_NONE, "Error in key handler\n");
                break;
            }
        }
 
        state->connected = 0;
-       V3_Close_Socket(state->client_fd);
+       v3_socket_close(state->client_fd);
     }
     
     return -1;
@@ -529,7 +530,13 @@ static int cons_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     struct vm_device * frontend = v3_find_dev(vm, v3_cfg_val(frontend_cfg, "tag"));
     char * dev_id = v3_cfg_val(cfg, "ID");
 
+    if (!state) {
+       PrintError(vm, VCORE_NONE, "Cannot allocate in init\n");
+       return -1;
+    }
+
 
+    state->vm = vm;
     state->server_fd = 0;
     state->client_fd = 0;
     state->frontend_dev = frontend;
@@ -537,17 +544,18 @@ static int cons_init(struct v3_vm_info * vm, v3_cfg_tree_t * cfg) {
     v3_lock_init(&(state->cons_lock));
 
 
-    struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, state);
+    struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, state);
 
-    if (v3_attach_device(vm, dev) == -1) {
-       PrintError("Could not attach device %s\n", dev_id);
+    if (dev == NULL) {
+       PrintError(vm, VCORE_NONE, "Could not attach device %s\n", dev_id);
+       V3_Free(state);
        return -1;
     }
 
 
-    v3_console_register_cga(frontend, &cons_ops, dev);
+    v3_console_register_cga(frontend, &cons_ops, state);
 
-    V3_CREATE_THREAD(cons_server, dev, "Telnet Console Network Server");
+    V3_CREATE_AND_START_THREAD(cons_server, state, "Telnet Console Network Server", 0);
 
     return 0;
 }