|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +from construct import Struct, Int8ul, Int16ul, Int32sl, Int32ul, Int64ul |
| 3 | +from subprocess import Popen, PIPE |
| 4 | +import pathlib |
| 5 | +import struct |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +from ..utils import * |
| 10 | + |
| 11 | +VirtioConfig = Struct( |
| 12 | + "irq" / Int32sl, |
| 13 | + "devid" / Int32ul, |
| 14 | + "feats" / Int64ul, |
| 15 | + "num_qus" / Int32ul, |
| 16 | + "data" / Int64ul, |
| 17 | + "data_len" / Int64ul, |
| 18 | + "verbose" / Int8ul, |
| 19 | +) |
| 20 | + |
| 21 | +class VirtioDescFlags(Register16): |
| 22 | + WRITE = 1 |
| 23 | + NEXT = 0 |
| 24 | + |
| 25 | +VirtioDesc = Struct( |
| 26 | + "addr" / Int64ul, |
| 27 | + "len" / Int32ul, |
| 28 | + "flags" / RegAdapter(VirtioDescFlags), |
| 29 | + "next" / Int16ul, |
| 30 | +) |
| 31 | + |
| 32 | +VirtioExcInfo = Struct( |
| 33 | + "devbase" / Int64ul, |
| 34 | + "qu" / Int16ul, |
| 35 | + "idx" / Int16ul, |
| 36 | + "pad" / Int32ul, |
| 37 | + "descbase" / Int64ul, |
| 38 | +) |
| 39 | + |
| 40 | +class VirtioDev: |
| 41 | + def __init__(self): |
| 42 | + self.base, self.hv = None, None # assigned by HV object |
| 43 | + |
| 44 | + def read_buf(self, desc): |
| 45 | + return self.hv.iface.readmem(desc.addr, desc.len) |
| 46 | + |
| 47 | + def read_desc(self, ctx, idx): |
| 48 | + off = VirtioDesc.sizeof() * idx |
| 49 | + return self.hv.iface.readstruct(ctx.descbase + off, VirtioDesc) |
| 50 | + |
| 51 | + @property |
| 52 | + def config_data(self): |
| 53 | + return b"" |
| 54 | + |
| 55 | + @property |
| 56 | + def devid(self): |
| 57 | + return 0 |
| 58 | + |
| 59 | + @property |
| 60 | + def num_qus(self): |
| 61 | + return 1 |
| 62 | + |
| 63 | + @property |
| 64 | + def feats(self): |
| 65 | + return 0 |
| 66 | + |
| 67 | +class Virtio9PTransport(VirtioDev): |
| 68 | + def __init__(self, tag="m1n1", root=None): |
| 69 | + p_stdin, self.fin = os.pipe() |
| 70 | + self.fout, p_stdout = os.pipe() |
| 71 | + if root is None: |
| 72 | + root = str(pathlib.Path(__file__).resolve().parents[3]) |
| 73 | + if type(tag) is str: |
| 74 | + self.tag = tag.encode("ascii") |
| 75 | + else: |
| 76 | + self.tag = tag |
| 77 | + self.p = Popen([ |
| 78 | + "u9fs", |
| 79 | + "-a", "none", # no auth |
| 80 | + "-n", # not a network conn |
| 81 | + "-u", os.getlogin(), # single user |
| 82 | + root, |
| 83 | + ], stdin=p_stdin, stdout=p_stdout, stderr=sys.stderr) |
| 84 | + |
| 85 | + @property |
| 86 | + def config_data(self): |
| 87 | + return struct.pack("=H", len(self.tag)) + self.tag |
| 88 | + |
| 89 | + @property |
| 90 | + def devid(self): |
| 91 | + return 9 |
| 92 | + |
| 93 | + @property |
| 94 | + def num_qus(self): |
| 95 | + return 1 |
| 96 | + |
| 97 | + @property |
| 98 | + def feats(self): |
| 99 | + return 1 |
| 100 | + |
| 101 | + def call(self, req): |
| 102 | + os.write(self.fin, req) |
| 103 | + resp = os.read(self.fout, 4) |
| 104 | + length = int.from_bytes(resp, byteorder="little") |
| 105 | + resp += os.read(self.fout, length - 4) |
| 106 | + return resp |
| 107 | + |
| 108 | + def handle_exc(self, ctx): |
| 109 | + head = self.read_desc(ctx, ctx.idx) |
| 110 | + assert not head.flags.WRITE |
| 111 | + |
| 112 | + req = bytearray() |
| 113 | + |
| 114 | + while not head.flags.WRITE: |
| 115 | + req += self.read_buf(head) |
| 116 | + |
| 117 | + if not head.flags.NEXT: |
| 118 | + break |
| 119 | + head = self.read_desc(ctx, head.next) |
| 120 | + |
| 121 | + resp = self.call(bytes(req)) |
| 122 | + resplen = len(resp) |
| 123 | + |
| 124 | + while len(resp): |
| 125 | + self.hv.iface.writemem(head.addr, resp[:head.len]) |
| 126 | + resp = resp[head.len:] |
| 127 | + if not head.flags.NEXT: |
| 128 | + break |
| 129 | + head = self.read_desc(ctx, head.next) |
| 130 | + |
| 131 | + self.hv.p.virtio_put_buffer(ctx.devbase, ctx.qu, ctx.idx, resplen) |
| 132 | + |
| 133 | + return True |
0 commit comments