Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/pmaydell/tags/pull-misc-2018121…
Browse files Browse the repository at this point in the history
…4' into staging

miscellaneous patches:
 * checkpatch.pl: Enforce multiline comment syntax
 * Rename cpu_physical_memory_write_rom() to address_space_write_rom()
 * disas, monitor, elf_ops: Use address_space_read() to read memory
 * Remove load_image() in favour of load_image_size()
 * Fix some minor memory leaks in arm boards/devices
 * virt: fix broken indentation

# gpg: Signature made Fri 14 Dec 2018 14:41:20 GMT
# gpg:                using RSA key 3C2525ED14360CDE
# gpg: Good signature from "Peter Maydell <peter.maydell@linaro.org>"
# gpg:                 aka "Peter Maydell <pmaydell@gmail.com>"
# gpg:                 aka "Peter Maydell <pmaydell@chiark.greenend.org.uk>"
# Primary key fingerprint: E1A5 C593 CD41 9DE2 8E83  15CF 3C25 25ED 1436 0CDE

* remotes/pmaydell/tags/pull-misc-20181214: (22 commits)
  virt: Fix broken indentation
  target/arm: Create timers in realize, not init
  tests/test-arm-mptimer: Don't leak string memory
  hw/sd/sdhci: Don't leak memory region in sdhci_sysbus_realize()
  hw/arm/mps2-tz.c: Free mscname string in make_dma()
  target/arm: Free name string in ARMCPRegInfo hashtable entries
  include/hw/loader.h: Document load_image_size()
  hw/core/loader.c: Remove load_image()
  device_tree.c: Don't use load_image()
  hw/block/tc58128.c: Don't use load_image()
  hw/i386/multiboot.c: Don't use load_image()
  hw/i386/pc.c: Don't use load_image()
  hw/pci/pci.c: Don't use load_image()
  hw/smbios/smbios.c: Don't use load_image()
  hw/ppc/ppc405_boards: Don't use load_image()
  hw/ppc/mac_newworld, mac_oldworld: Don't use load_image()
  elf_ops.h: Use address_space_write() to write memory
  monitor: Use address_space_read() to read memory
  disas.c: Use address_space_read() to read memory
  Rename cpu_physical_memory_write_rom() to address_space_write_rom()
  ...

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
pm215 committed Dec 16, 2018
2 parents 58b1f0f + bbac02f commit b019f5e
Show file tree
Hide file tree
Showing 27 changed files with 287 additions and 198 deletions.
2 changes: 1 addition & 1 deletion device_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void *load_device_tree(const char *filename_path, int *sizep)
/* First allocate space in qemu for device tree */
fdt = g_malloc0(dt_size);

dt_file_load_size = load_image(filename_path, fdt);
dt_file_load_size = load_image_size(filename_path, fdt, dt_size);
if (dt_file_load_size < 0) {
error_report("Unable to open device tree file '%s'",
filename_path);
Expand Down
5 changes: 4 additions & 1 deletion disas.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,10 @@ static int
physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length,
struct disassemble_info *info)
{
cpu_physical_memory_read(memaddr, myaddr, length);
CPUDebug *s = container_of(info, CPUDebug, info);

address_space_read(s->cpu->as, memaddr, MEMTXATTRS_UNSPECIFIED,
myaddr, length);
return 0;
}

Expand Down
35 changes: 16 additions & 19 deletions docs/devel/loads-stores.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ Regexes for git grep
- ``\<address_space_ldu\?[bwql]\(_[lb]e\)\?\>``
- ``\<address_space_st[bwql]\(_[lb]e\)\?\>``

``address_space_write_rom``
~~~~~~~~~~~~~~~~~~~~~~~~~~~

This function performs a write by physical address like
``address_space_write``, except that if the write is to a ROM then
the ROM contents will be modified, even though a write by the guest
CPU to the ROM would be ignored. This is used for non-guest writes
like writes from the gdb debug stub or initial loading of ROM contents.

Note that portions of the write which attempt to write data to a
device will be silently ignored -- only real RAM and ROM will
be written to.

Regexes for git grep
- ``address_space_write_rom``

``{ld,st}*_phys``
~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -315,25 +331,6 @@ For new code they are better avoided:
Regexes for git grep
- ``\<cpu_physical_memory_\(read\|write\|rw\)\>``

``cpu_physical_memory_write_rom``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This function performs a write by physical address like
``address_space_write``, except that if the write is to a ROM then
the ROM contents will be modified, even though a write by the guest
CPU to the ROM would be ignored.

Note that unlike ``cpu_physical_memory_write()`` this function takes
an AddressSpace argument, but unlike ``address_space_write()`` this
function does not take a ``MemTxAttrs`` or return a ``MemTxResult``.

**TODO**: we should probably clean up this inconsistency and
turn the function into ``address_space_write_rom`` with an API
matching ``address_space_write``.

``cpu_physical_memory_write_rom``


``cpu_memory_rw_debug``
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
30 changes: 19 additions & 11 deletions exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -3388,8 +3388,12 @@ enum write_rom_type {
FLUSH_CACHE,
};

static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
hwaddr addr, const uint8_t *buf, int len, enum write_rom_type type)
static inline MemTxResult address_space_write_rom_internal(AddressSpace *as,
hwaddr addr,
MemTxAttrs attrs,
const uint8_t *buf,
int len,
enum write_rom_type type)
{
hwaddr l;
uint8_t *ptr;
Expand All @@ -3399,8 +3403,7 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
rcu_read_lock();
while (len > 0) {
l = len;
mr = address_space_translate(as, addr, &addr1, &l, true,
MEMTXATTRS_UNSPECIFIED);
mr = address_space_translate(as, addr, &addr1, &l, true, attrs);

if (!(memory_region_is_ram(mr) ||
memory_region_is_romd(mr))) {
Expand All @@ -3423,13 +3426,16 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,
addr += l;
}
rcu_read_unlock();
return MEMTX_OK;
}

/* used for ROM loading : can write in RAM and ROM */
void cpu_physical_memory_write_rom(AddressSpace *as, hwaddr addr,
const uint8_t *buf, int len)
MemTxResult address_space_write_rom(AddressSpace *as, hwaddr addr,
MemTxAttrs attrs,
const uint8_t *buf, int len)
{
cpu_physical_memory_write_rom_internal(as, addr, buf, len, WRITE_DATA);
return address_space_write_rom_internal(as, addr, attrs,
buf, len, WRITE_DATA);
}

void cpu_flush_icache_range(hwaddr start, int len)
Expand All @@ -3444,8 +3450,9 @@ void cpu_flush_icache_range(hwaddr start, int len)
return;
}

cpu_physical_memory_write_rom_internal(&address_space_memory,
start, NULL, len, FLUSH_CACHE);
address_space_write_rom_internal(&address_space_memory,
start, MEMTXATTRS_UNSPECIFIED,
NULL, len, FLUSH_CACHE);
}

typedef struct {
Expand Down Expand Up @@ -3873,8 +3880,9 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,
l = len;
phys_addr += (addr & ~TARGET_PAGE_MASK);
if (is_write) {
cpu_physical_memory_write_rom(cpu->cpu_ases[asidx].as,
phys_addr, buf, l);
address_space_write_rom(cpu->cpu_ases[asidx].as, phys_addr,
MEMTXATTRS_UNSPECIFIED,
buf, l);
} else {
address_space_rw(cpu->cpu_ases[asidx].as, phys_addr,
MEMTXATTRS_UNSPECIFIED,
Expand Down
1 change: 1 addition & 0 deletions hw/arm/mps2-tz.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ static MemoryRegion *make_dma(MPS2TZMachineState *mms, void *opaque,
sysbus_connect_irq(s, 2, qdev_get_gpio_in_named(iotkitdev,
"EXP_IRQ", 57 + i * 3));

g_free(mscname);
return sysbus_mmio_get_region(s, 0);
}

Expand Down
2 changes: 1 addition & 1 deletion hw/arm/virt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1854,7 +1854,7 @@ static const TypeInfo virt_machine_info = {
.instance_size = sizeof(VirtMachineState),
.class_size = sizeof(VirtMachineClass),
.class_init = virt_machine_class_init,
.instance_init = virt_instance_init,
.instance_init = virt_instance_init,
.interfaces = (InterfaceInfo[]) {
{ TYPE_HOTPLUG_HANDLER },
{ }
Expand Down
3 changes: 2 additions & 1 deletion hw/block/tc58128.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ static void init_dev(tc58128_dev * dev, const char *filename)
memset(dev->flash_contents, 0xff, FLASH_SIZE);
if (filename) {
/* Load flash image skipping the first block */
ret = load_image(filename, dev->flash_contents + 528 * 32);
ret = load_image_size(filename, dev->flash_contents + 528 * 32,
FLASH_SIZE - 528 * 32);
if (ret < 0) {
if (!qtest_enabled()) {
error_report("Could not load flash image %s", filename);
Expand Down
29 changes: 2 additions & 27 deletions hw/core/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,6 @@ int64_t get_image_size(const char *filename)
return size;
}

/* return the size or -1 if error */
/* deprecated, because caller does not specify buffer size! */
int load_image(const char *filename, uint8_t *addr)
{
int fd, size;
fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0)
return -1;
size = lseek(fd, 0, SEEK_END);
if (size == -1) {
fprintf(stderr, "file %-20s: get size error: %s\n",
filename, strerror(errno));
close(fd);
return -1;
}

lseek(fd, 0, SEEK_SET);
if (read(fd, addr, size) != size) {
close(fd);
return -1;
}
close(fd);
return size;
}

/* return the size or -1 if error */
ssize_t load_image_size(const char *filename, void *addr, size_t size)
{
Expand Down Expand Up @@ -1103,8 +1078,8 @@ static void rom_reset(void *unused)
void *host = memory_region_get_ram_ptr(rom->mr);
memcpy(host, rom->data, rom->datasize);
} else {
cpu_physical_memory_write_rom(rom->as, rom->addr, rom->data,
rom->datasize);
address_space_write_rom(rom->as, rom->addr, MEMTXATTRS_UNSPECIFIED,
rom->data, rom->datasize);
}
if (rom->isrom) {
/* rom needs to be written only once */
Expand Down
6 changes: 5 additions & 1 deletion hw/i386/multiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ int load_multiboot(FWCfgState *fw_cfg,
mbs.mb_buf_size = TARGET_PAGE_ALIGN(mb_mod_length + mbs.mb_buf_size);
mbs.mb_buf = g_realloc(mbs.mb_buf, mbs.mb_buf_size);

load_image(one_file, (unsigned char *)mbs.mb_buf + offs);
if (load_image_size(one_file, (unsigned char *)mbs.mb_buf + offs,
mbs.mb_buf_size - offs) < 0) {
error_report("Error loading file '%s'", one_file);
exit(1);
}
mb_add_mod(&mbs, mbs.mb_buf_phys + offs,
mbs.mb_buf_phys + offs + mb_mod_length, c);

Expand Down
22 changes: 12 additions & 10 deletions hw/i386/pc.c
Original file line number Diff line number Diff line change
Expand Up @@ -839,10 +839,9 @@ static void load_linux(PCMachineState *pcms,
{
uint16_t protocol;
int setup_size, kernel_size, cmdline_size;
int64_t initrd_size = 0;
int dtb_size, setup_data_offset;
uint32_t initrd_max;
uint8_t header[8192], *setup, *kernel, *initrd_data;
uint8_t header[8192], *setup, *kernel;
hwaddr real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
FILE *f;
char *vmode;
Expand Down Expand Up @@ -965,27 +964,30 @@ static void load_linux(PCMachineState *pcms,

/* load initrd */
if (initrd_filename) {
gsize initrd_size;
gchar *initrd_data;
GError *gerr = NULL;

if (protocol < 0x200) {
fprintf(stderr, "qemu: linux kernel too old to load a ram disk\n");
exit(1);
}

initrd_size = get_image_size(initrd_filename);
if (initrd_size < 0) {
if (!g_file_get_contents(initrd_filename, &initrd_data,
&initrd_size, &gerr)) {
fprintf(stderr, "qemu: error reading initrd %s: %s\n",
initrd_filename, strerror(errno));
initrd_filename, gerr->message);
exit(1);
} else if (initrd_size >= initrd_max) {
}
if (initrd_size >= initrd_max) {
fprintf(stderr, "qemu: initrd is too large, cannot support."
"(max: %"PRIu32", need %"PRId64")\n", initrd_max, initrd_size);
"(max: %"PRIu32", need %"PRId64")\n",
initrd_max, (uint64_t)initrd_size);
exit(1);
}

initrd_addr = (initrd_max-initrd_size) & ~4095;

initrd_data = g_malloc(initrd_size);
load_image(initrd_filename, initrd_data);

fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
Expand Down
7 changes: 4 additions & 3 deletions hw/intc/apic.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ static void apic_sync_vapic(APICCommonState *s, int sync_type)
}
vapic_state.irr = vector & 0xff;

cpu_physical_memory_write_rom(&address_space_memory,
s->vapic_paddr + start,
((void *)&vapic_state) + start, length);
address_space_write_rom(&address_space_memory,
s->vapic_paddr + start,
MEMTXATTRS_UNSPECIFIED,
((void *)&vapic_state) + start, length);
}
}

Expand Down
2 changes: 1 addition & 1 deletion hw/misc/tz-mpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static int tz_mpc_attrs_to_index(IOMMUMemoryRegion *iommu, MemTxAttrs attrs)
{
/* We treat unspecified attributes like secure. Transactions with
* unspecified attributes come from places like
* cpu_physical_memory_write_rom() for initial image load, and we want
* rom_reset() for initial image load, and we want
* those to pass through the from-reset "everything is secure" config.
* All the real during-emulation transactions from the CPU will
* specify attributes.
Expand Down
6 changes: 5 additions & 1 deletion hw/pci/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,11 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
pdev->has_rom = true;
memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, size, &error_fatal);
ptr = memory_region_get_ram_ptr(&pdev->rom);
load_image(path, ptr);
if (load_image_size(path, ptr, size) < 0) {
error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile);
g_free(path);
return;
}
g_free(path);

if (is_default_rom) {
Expand Down
10 changes: 4 additions & 6 deletions hw/ppc/mac_newworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ static void ppc_core99_init(MachineState *machine)
MACIOIDEState *macio_ide;
BusState *adb_bus;
MacIONVRAMState *nvr;
int bios_size, ndrv_size;
uint8_t *ndrv_file;
int bios_size;
int ppc_boot_device;
DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
void *fw_cfg;
Expand Down Expand Up @@ -510,11 +509,10 @@ static void ppc_core99_init(MachineState *machine)
/* MacOS NDRV VGA driver */
filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, NDRV_VGA_FILENAME);
if (filename) {
ndrv_size = get_image_size(filename);
if (ndrv_size != -1) {
ndrv_file = g_malloc(ndrv_size);
ndrv_size = load_image(filename, ndrv_file);
gchar *ndrv_file;
gsize ndrv_size;

if (g_file_get_contents(filename, &ndrv_file, &ndrv_size, NULL)) {
fw_cfg_add_file(fw_cfg, "ndrv/qemu_vga.ndrv", ndrv_file, ndrv_size);
}
g_free(filename);
Expand Down
10 changes: 4 additions & 6 deletions hw/ppc/mac_oldworld.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ static void ppc_heathrow_init(MachineState *machine)
SysBusDevice *s;
DeviceState *dev, *pic_dev;
BusState *adb_bus;
int bios_size, ndrv_size;
uint8_t *ndrv_file;
int bios_size;
uint16_t ppc_boot_device;
DriveInfo *hd[MAX_IDE_BUS * MAX_IDE_DEVS];
void *fw_cfg;
Expand Down Expand Up @@ -361,11 +360,10 @@ static void ppc_heathrow_init(MachineState *machine)
/* MacOS NDRV VGA driver */
filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, NDRV_VGA_FILENAME);
if (filename) {
ndrv_size = get_image_size(filename);
if (ndrv_size != -1) {
ndrv_file = g_malloc(ndrv_size);
ndrv_size = load_image(filename, ndrv_file);
gchar *ndrv_file;
gsize ndrv_size;

if (g_file_get_contents(filename, &ndrv_file, &ndrv_size, NULL)) {
fw_cfg_add_file(fw_cfg, "ndrv/qemu_vga.ndrv", ndrv_file, ndrv_size);
}
g_free(filename);
Expand Down
12 changes: 8 additions & 4 deletions hw/ppc/ppc405_boards.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,11 @@ static void ref405ep_init(MachineState *machine)
bios_name = BIOS_FILENAME;
filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
if (filename) {
bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
bios_size = load_image_size(filename,
memory_region_get_ram_ptr(bios),
BIOS_SIZE);
g_free(filename);
if (bios_size < 0 || bios_size > BIOS_SIZE) {
if (bios_size < 0) {
error_report("Could not load PowerPC BIOS '%s'", bios_name);
exit(1);
}
Expand Down Expand Up @@ -515,9 +517,11 @@ static void taihu_405ep_init(MachineState *machine)
&error_fatal);
filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
if (filename) {
bios_size = load_image(filename, memory_region_get_ram_ptr(bios));
bios_size = load_image_size(filename,
memory_region_get_ram_ptr(bios),
BIOS_SIZE);
g_free(filename);
if (bios_size < 0 || bios_size > BIOS_SIZE) {
if (bios_size < 0) {
error_report("Could not load PowerPC BIOS '%s'", bios_name);
exit(1);
}
Expand Down
Loading

0 comments on commit b019f5e

Please sign in to comment.