aboutsummaryrefslogtreecommitdiff
path: root/hw/hypercall.c
blob: a1f629ca439314bd40c46fc8c46b4765344b952c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * QEMU-KVM Hypercall emulation
 * 
 * Copyright (c) 2003-2004 Fabrice Bellard
 * Copyright (c) 2006 Qumranet
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "vl.h"
#include <stddef.h>

#define HP_CMD          0x00  // The command register  WR
#define HP_ISRSTATUS    0x04  // Interrupt status reg RD 
#define HP_TXSIZE       0x08
#define HP_TXBUFF       0x0c
#define HP_RXSIZE       0x10
#define HP_RXBUFF       0x14

// HP_CMD register commands
#define HP_CMD_DI		1 // disable interrupts
#define HP_CMD_EI		2 // enable interrupts
#define HP_CMD_RESET	4 // enable interrupts


/* Bits in HP_ISR - Interrupt status register */
#define HPISR_RX	0x01  // Data is ready to be read

int use_hypercall_dev = 0;
static CharDriverState *vmchannel_hd;

#define HP_MEM_SIZE    0xE0

typedef struct HypercallState {
    uint32_t cmd;
    uint32_t isr;
    uint32_t txsize;
    uint32_t txbuff;
    uint32_t rxsize;
    uint8_t  RxBuff[HP_MEM_SIZE];
    uint8_t  txbufferaccu[HP_MEM_SIZE];
    int      txbufferaccu_offset;
    int      irq;
    PCIDevice *pci_dev;
} HypercallState;

HypercallState *pHypercallState = NULL;

static void hp_reset(HypercallState *s)
{
    s->cmd = 0;
    s->isr = 0;
    s->txsize = 0;
    s->txbuff = 0;
    s->rxsize= 0;
    s->txbufferaccu_offset = 0;
}

static void hp_ioport_write(void *opaque, uint32_t addr, uint32_t val)
{
    HypercallState *s = opaque;

    //printf("hp_ioport_write,addr=0x%x, val=0x%x\n",addr, val);

    addr &= 0xff;

    switch(addr)
    {
        case HP_CMD:
        {
            s->cmd = val;
            if (val == HP_CMD_RESET){
                hp_reset(s);
                return;
            }
            break;
        }

        case HP_TXSIZE:
        {
            // handle the case when the we are being called when txsize is not 0
            if (s->txsize != 0) {
                printf("txsize is being set, but txsize is not 0!!!\n");
            }
            if (val > HP_MEM_SIZE) {
                printf("txsize is larger than allowed by hw!!!\n");
            }
            s->txsize = val;
            s->txbufferaccu_offset = 0;
            break;
        }

        case HP_TXBUFF:
        {
            if (s->txsize == 0) {
                printf("error with txbuff!!!\n");
                break;
            }

            s->txbufferaccu[s->txbufferaccu_offset] = val;
            s->txbufferaccu_offset++;
            if (s->txbufferaccu_offset >= s->txsize) {
                printf("tranmit txbuf, Len:0x%x\n", s->txbufferaccu_offset);
                qemu_chr_write(vmchannel_hd, s->txbufferaccu, s->txsize);
                s->txbufferaccu_offset = 0;
                s->txsize = 0;
            }
            break;
        }
        default:
        {
            printf("hp_ioport_write to unhandled address!!!\n");
        }
    }
}

static uint32_t hp_ioport_read(void *opaque, uint32_t addr)
{
    HypercallState *s = opaque;
    int ret;

    if (addr != 0xc204) {
        //printf("hp_ioport_read addr:0x%x\n",addr);
    }

    addr &= 0xff;

    if (addr >= offsetof(HypercallState, RxBuff) )
    {
        int RxBuffOffset = addr - (offsetof(HypercallState, RxBuff));
        ret = s->RxBuff[RxBuffOffset];
        return ret;
    }

    switch (addr)
    {
    case HP_ISRSTATUS:
        if (s->isr != 0){
            printf("hp_ioport_read s->isr=0x%x\n", s->isr);
        }
        ret = s->isr;
        if (ret & HPISR_RX) {
            s->isr &= ~HPISR_RX;
        }
        break;
    case HP_RXSIZE:
        ret = s->rxsize;
        break;

    default:
        ret = 0x00;
        break;
    }

    return ret;
}

/***********************************************************/
/* PCI Hypercall definitions */

typedef struct PCIHypercallState {
    PCIDevice dev;
    HypercallState hp;
} PCIHypercallState;

static void hp_map(PCIDevice *pci_dev, int region_num, 
                       uint32_t addr, uint32_t size, int type)
{
    PCIHypercallState *d = (PCIHypercallState *)pci_dev;
    HypercallState *s = &d->hp;

    register_ioport_write(addr, 0x100, 1, hp_ioport_write, s);
    register_ioport_read(addr, 0x100, 1, hp_ioport_read, s);

}


static void hypercall_update_irq(HypercallState *s)
{
    printf("hypercall_update_irq\n");

    if (s->cmd &= HP_CMD_DI) {
        return;
    }
	/* PCI irq */
	pci_set_irq(s->pci_dev, 0, 1);
}

void pci_hypercall_init(PCIBus *bus)
{
    PCIHypercallState *d;
    HypercallState *s;
    uint8_t *pci_conf;

    // If the vmchannel wasn't initialized, we don't want the Hypercall device in the guest
    if (use_hypercall_dev == 0) {
        return;
    }

    d = (PCIHypercallState *)pci_register_device(bus,
                                                 "Hypercall", sizeof(PCIHypercallState),
                                                 -1,
                                                 NULL, NULL);

    pci_conf = d->dev.config;
    pci_conf[0x00] = 0x02; // Qumranet vendor ID 0x5002
    pci_conf[0x01] = 0x50;
    pci_conf[0x02] = 0x58; // Qumranet DeviceID 0x2258
    pci_conf[0x03] = 0x22;

    pci_conf[0x09] = 0x00; // ProgIf
    pci_conf[0x0a] = 0x00; // SubClass
    pci_conf[0x0b] = 0x05; // BaseClass

    pci_conf[0x0e] = 0x00; // header_type
    pci_conf[0x3d] = 1; // interrupt pin 0

    pci_register_io_region(&d->dev, 0, 0x100,
                           PCI_ADDRESS_SPACE_IO, hp_map);
    s = &d->hp;
    pHypercallState = s;
    s->irq = 16; /* PCI interrupt */
    s->pci_dev = (PCIDevice *)d;

    hp_reset(s);
}


static int vmchannel_can_read(void *opaque)
{
    return 128;
}

// input from vmchannel outside caller
static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
{
    int i;
    
    printf("vmchannel_read buf:%p, size:%d\n", buf, size);
    for(i = 0; i < size; i++) {
        printf("%x,", buf[i]);
    }
    printf("\n");

    // if the hypercall device is in interrupts disabled state, don't accept the data
    if (pHypercallState->cmd &= HP_CMD_DI) {
        return;
    }

    for(i = 0; i < size; i++) {
        //printf("buf[i%d]=%x\n",i, buf[i]);
        pHypercallState->RxBuff[i] = buf[i];
    }
    pHypercallState->rxsize = size;
    pHypercallState->isr = HPISR_RX;
    hypercall_update_irq(pHypercallState);
}

void vmchannel_init(CharDriverState *hd)
{
    vmchannel_hd = hd;

    //printf("vmchannel_init\n");
    use_hypercall_dev = 1;
    qemu_chr_add_read_handler(vmchannel_hd, vmchannel_can_read, vmchannel_read, &pHypercallState);

}