Skip to contentSkip to conversation

Configuration device: add write channel, mark pages RO

RFC PATCH In review Blue Swirl 2 participants 2 messages Sep 7, 2008 – Sep 8, 2008

lore
Share & tools
lore

Blue Swirl

author
#0 lore raw

RFC PATCH 1/1 Configuration device: add write channel, mark pages RO

Hi,

The first patch extend the configuration device with a guest write capability. A callback is invoked when the buffer is full.

It's a bit odd to get completely different behavior when doing I/O to the same port just because of the word size used in the IO. I'd rather have two ports, one for control write or status read and another for data read/write. Is two ports too much to ask on a PC?

The second patch uses the first one to add a new key for marking guest pages read only. This patch was inspired due to discussion I saw on KernelTrap:
http://kerneltrap.org/mailarchive/linux-kernel/2008/9/4/3191234

hw/fw_cfg.c +56 -6
Index: qemu/hw/fw_cfg.c
===================================================================
@@ -40,7 +40,9 @@
 
 typedef struct _FWCfgEntry {
     uint16_t len;
-    const uint8_t *data;
+    uint8_t *data;
+    void *callback_opaque;
+    FWCfgCallback callback;
 } FWCfgEntry;
 
 typedef struct _FWCfgState {
@@ -49,12 +51,28 @@
     uint16_t cur_offset;
 } FWCfgState;
 
+static void fw_cfg_write(FWCfgState *s, uint8_t value)
+{
+    int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
+    FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
+
+    FW_CFG_DPRINTF("write %d\n", value);
+
+    if (s->cur_entry & FW_CFG_WRITE_CHANNEL && s->cur_offset < e->len) {
+        e->data[s->cur_offset++] = value;
+        if (s->cur_offset == e->len) {
+            e->callback(e->callback_opaque, e->data);
+            s->cur_offset = 0;
+        }
+    }
+}
+
 static int fw_cfg_select(FWCfgState *s, uint16_t key)
 {
     int ret;
 
     s->cur_offset = 0;
-    if ((key & ~FW_CFG_ARCH_LOCAL) >= FW_CFG_MAX_ENTRY) {
+    if ((key & FW_CFG_ENTRY_MASK) >= FW_CFG_MAX_ENTRY) {
         s->cur_entry = FW_CFG_INVALID;
         ret = 0;
     } else {
@@ -70,7 +88,7 @@
 static uint8_t fw_cfg_read(FWCfgState *s)
 {
     int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
-    FWCfgEntry *e = &s->entries[arch][s->cur_entry & ~FW_CFG_ARCH_LOCAL];
+    FWCfgEntry *e = &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];
     uint8_t ret;
 
     if (s->cur_entry == FW_CFG_INVALID || !e->data || s->cur_offset >= e->len)
@@ -88,6 +106,11 @@
     return fw_cfg_read(opaque);
 }
 
+static void fw_cfg_io_writeb(void *opaque, uint32_t addr, uint32_t value)
+{
+    return fw_cfg_write(opaque, (uint8_t)value);
+}
+
 static void fw_cfg_io_writew(void *opaque, uint32_t addr, uint32_t value)
 {
     fw_cfg_select(opaque, (uint16_t)value);
@@ -98,6 +121,12 @@
     return fw_cfg_read(opaque);
 }
 
+static void fw_cfg_mem_writeb(void *opaque, target_phys_addr_t addr,
+                              uint32_t value)
+{
+    return fw_cfg_write(opaque, (uint8_t)value);
+}
+
 static void fw_cfg_mem_writew(void *opaque, target_phys_addr_t addr,
                               uint32_t value)
 {
@@ -111,7 +140,7 @@
 };
 
 static CPUWriteMemoryFunc *fw_cfg_mem_write[3] = {
-    NULL,
+    fw_cfg_mem_writeb,
     fw_cfg_mem_writew,
     NULL,
 };
@@ -144,12 +173,12 @@
     return 0;
 }
 
-int fw_cfg_add_bytes(void *opaque, uint16_t key, const uint8_t *data, uint16_t len)
+int fw_cfg_add_bytes(void *opaque, uint16_t key, uint8_t *data, uint16_t len)
 {
     FWCfgState *s = opaque;
     int arch = !!(key & FW_CFG_ARCH_LOCAL);
 
-    key &= (~FW_CFG_ARCH_LOCAL);
+    key &= FW_CFG_ENTRY_MASK;
 
     if (key >= FW_CFG_MAX_ENTRY)
         return 0;
@@ -193,6 +222,26 @@
     return fw_cfg_add_bytes(opaque, key, (uint8_t *)copy, sizeof(value));
 }
 
+int fw_cfg_add_callback(void *opaque, uint16_t key, FWCfgCallback callback,
+                        void *callback_opaque, uint8_t *data, size_t len)
+{
+    FWCfgState *s = opaque;
+    int arch = !!(key & FW_CFG_ARCH_LOCAL);
+
+    key &= FW_CFG_ENTRY_MASK;
+
+    if (key >= FW_CFG_MAX_ENTRY || !(key & FW_CFG_WRITE_CHANNEL)
+        || len > 65535)
+        return 0;
+
+    s->entries[arch][key].data = data;
+    s->entries[arch][key].len = len;
+    s->entries[arch][key].callback_opaque = callback_opaque;
+    s->entries[arch][key].callback = callback;
+
+    return 1;
+}
+
 void *fw_cfg_init(uint32_t port, target_phys_addr_t addr)
 {
     FWCfgState *s;
@@ -205,6 +254,7 @@
 
     if (port) {
         register_ioport_read(port, 1, 1, fw_cfg_io_readb, s);
+        register_ioport_write(port, 1, 1, fw_cfg_io_writeb, s);
         register_ioport_write(port, 2, 2, fw_cfg_io_writew, s);
     }
     if (addr) {
hw/fw_cfg.h +7 -2
Index: qemu/hw/fw_cfg.h
===================================================================
@@ -10,16 +10,21 @@
 #define FW_CFG_MACHINE_ID       0x06
 #define FW_CFG_MAX_ENTRY        0x10
 
+#define FW_CFG_WRITE_CHANNEL    0x4000
 #define FW_CFG_ARCH_LOCAL       0x8000
+#define FW_CFG_ENTRY_MASK       ~(FW_CFG_WRITE_CHANNEL | FW_CFG_ARCH_LOCAL)
 
 #define FW_CFG_INVALID          0xffff
 
 #ifndef NO_QEMU_PROTOS
-int fw_cfg_add_bytes(void *opaque, uint16_t key, const uint8_t *data,
-                     uint16_t len);
+typedef void (*FWCfgCallback)(void *opaque, uint8_t *data);
+
+int fw_cfg_add_bytes(void *opaque, uint16_t key, uint8_t *data, uint16_t len);
 int fw_cfg_add_i16(void *opaque, uint16_t key, uint16_t value);
 int fw_cfg_add_i32(void *opaque, uint16_t key, uint32_t value);
 int fw_cfg_add_i64(void *opaque, uint16_t key, uint64_t value);
+int fw_cfg_add_callback(void *opaque, uint16_t key, FWCfgCallback callback,
+                        void *callback_opaque, uint8_t *data, size_t len);
 void *fw_cfg_init(uint32_t port, target_phys_addr_t addr);
 #endif /* NO_QEMU_PROTOS */
 
hw/fw_cfg.c +10 -0
Index: qemu/hw/fw_cfg.c
===================================================================
@@ -242,11 +242,19 @@
     return 1;
 }
 
+static void fw_cfg_mark_page_ro(void *opaque, uint8_t *data)
+{
+    uint64_t page = *(uint64_t *)data;
+
+    mark_page_ro(page);
+}
+
 void *fw_cfg_init(uint32_t port, target_phys_addr_t addr)
 {
     FWCfgState *s;
     int io_memory;
     extern int nographic;
+    static uint64_t ro_page;
 
     s = qemu_mallocz(sizeof(FWCfgState));
     if (!s)
@@ -267,6 +275,8 @@
     fw_cfg_add_bytes(s, FW_CFG_UUID, qemu_uuid, 16);
     fw_cfg_add_i16(s, FW_CFG_NOGRAPHIC, (uint16_t)nographic);
     fw_cfg_add_i16(s, FW_CFG_NB_CPUS, (uint16_t)smp_cpus);
+    fw_cfg_add_callback(s, FW_CFG_RO_PAGES, fw_cfg_mark_page_ro, NULL,
+                        (uint8_t *)&ro_page, sizeof(ro_page));
 
     register_savevm("fw_cfg", -1, 1, fw_cfg_save, fw_cfg_load, s);
     qemu_register_reset(fw_cfg_reset, s);
hw/fw_cfg.h +1 -0
Index: qemu/hw/fw_cfg.h
===================================================================
@@ -8,6 +8,7 @@
 #define FW_CFG_NOGRAPHIC        0x04
 #define FW_CFG_NB_CPUS          0x05
 #define FW_CFG_MACHINE_ID       0x06
+#define FW_CFG_RO_PAGES         0x07
 #define FW_CFG_MAX_ENTRY        0x10
 
 #define FW_CFG_WRITE_CHANNEL    0x4000
exec.c +18 -0
Index: qemu/exec.c
===================================================================
@@ -449,6 +449,24 @@
     tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
 }
 
+void mark_page_ro(uint64_t page)
+{
+    PhysPageDesc *p;
+    unsigned long pd;
+
+    p = phys_page_find(page >> TARGET_PAGE_BITS);
+    if (!p)
+        return;
+
+    pd = p->phys_offset;
+
+    if ((pd & ~TARGET_PAGE_MASK) != IO_MEM_RAM &&
+        (pd & ~TARGET_PAGE_MASK) != IO_MEM_ROM)
+        return;
+    p->phys_offset &= TARGET_PAGE_MASK;
+    p->phys_offset |= IO_MEM_ROM;
+}
+
 /* Must be called before using the QEMU cpus. 'tb_size' is the size
    (in bytes) allocated to the translation buffer. Zero means default
    size. */
cpu-all.h +3 -0
Index: qemu/cpu-all.h
===================================================================
@@ -926,6 +926,9 @@
 {
     cpu_physical_memory_rw(addr, (uint8_t *)buf, len, 1);
 }
+
+void mark_page_ro(uint64_t page);
+
 uint32_t ldub_phys(target_phys_addr_t addr);
 uint32_t lduw_phys(target_phys_addr_t addr);
 uint32_t ldl_phys(target_phys_addr_t addr);
#1 lore raw
Quoted 5 lines · Blue Swirl

Hi,

The first patch extend the configuration device with a guest write capability. A callback is invoked when the buffer is full.

Looks useful. VMware back door allows for two way communication too.

Quoted 4 lines

It's a bit odd to get completely different behavior when doing I/O to the same port just because of the word size used in the IO. I'd rather have two ports, one for control write or status read and another for data read/write. Is two ports too much to ask on a PC?

Lets use two ports for clarity.

--
Gleb.