Skip to content

Commit

Permalink
Merge tag 'pull-target-arm-20240311' of https://git.linaro.org/people…
Browse files Browse the repository at this point in the history
…/pmaydell/qemu-arm into staging

target-arm queue:
 * contrib/elf2dmp: Improve robustness to corrupt input files
 * docs: update copyright date to the year 2024
 * hw/arm: Deprecate various old Arm machine types

# -----BEGIN PGP SIGNATURE-----
#
# iQJNBAABCAA3FiEE4aXFk81BneKOgxXPPCUl7RQ2DN4FAmXvV4gZHHBldGVyLm1h
# eWRlbGxAbGluYXJvLm9yZwAKCRA8JSXtFDYM3oe5EACvvve9LOJ7UA4teEbn8zzs
# O7GGsycpu3XWINX98sUi5Cuu+uexkcXsHf2Vg8GABj2fUuA+EEqPIdIWZhVcfB2t
# RYGPgat/JVTwRUsl38OQ97l4joI2JeW18B20Pmh4/2tbZCJuIHoiHxxb+3aQl6Nl
# gz/H137ayy+Q5utt1b6z1qXnw4etiUhk4wg2ltGVtAw63w5GZOvamTicSQ1DC3ZP
# zfbIKW2WpGNjzrC1tCvaW/dNojt2blpZbsnpdPsL8cNE8puo01FnFLsUZCzOtu3m
# 23uoQq0KjQZBJrU2oZMtshc+sKT/FGPADJ9B+J8jjU0EY6cV+Qo7FT4E78mxzoR0
# JbY9SLATtY7RE1Fbh3/Am+3OEyb3ZDor5nCux0CWOsuFoBk96dzD7r5MXxM2eft1
# pGmdJStYysZkdlSyx61bu6OifHOaGjnOe+lYWpaCrVy1U3cO3hbMWH2siSQygppM
# 8EfjyfadzfST+nAnXfduSgWMv7Nc4ql4GIOxVnMIfGig32PIp545IvM9neh6GIp/
# 8fzw6TdoCQkHcWaazV1ibPF0ceH6JwRvLIMkWlNpr/QBSNdsx+zkdh7WZD+3S91U
# XrCAA7hgf7OIvHauSD0ucSbztIiFRMROcHxIoh0ui6BermtaD6fnlei4QcsJI17o
# 6XZSNSWm5/+JEsn+dcVh4g==
# =UHyi
# -----END PGP SIGNATURE-----
# gpg: Signature made Mon 11 Mar 2024 19:12:08 GMT
# gpg:                using RSA key E1A5C593CD419DE28E8315CF3C2525ED14360CDE
# gpg:                issuer "peter.maydell@linaro.org"
# gpg: Good signature from "Peter Maydell <peter.maydell@linaro.org>" [ultimate]
# gpg:                 aka "Peter Maydell <pmaydell@gmail.com>" [ultimate]
# gpg:                 aka "Peter Maydell <pmaydell@chiark.greenend.org.uk>" [ultimate]
# gpg:                 aka "Peter Maydell <peter@archaic.org.uk>" [ultimate]
# Primary key fingerprint: E1A5 C593 CD41 9DE2 8E83  15CF 3C25 25ED 1436 0CDE

* tag 'pull-target-arm-20240311' of https://git.linaro.org/people/pmaydell/qemu-arm:
  docs: update copyright date to the year 2024
  contrib/elf2dmp: Ensure phdrs fit in file
  contrib/elf2dmp: Clamp QEMU note to file size
  contrib/elf2dmp: Use GPtrArray
  MAINTAINERS: Add Akihiko Odaki as a elf2dmp reviewer
  contrib/elf2dmp: Use rol64() to decode
  contrib/elf2dmp: Use lduw_le_p() to read PDB
  contrib/elf2dmp: Ensure segment fits in file
  contrib/elf2dmp: Always destroy PA space
  contrib/elf2dmp: Always check for PA resolution failure
  contrib/elf2dmp: Fix error reporting style in main.c
  contrib/elf2dmp: Fix error reporting style in qemu_elf.c
  contrib/elf2dmp: Fix error reporting style in pdb.c
  contrib/elf2dmp: Fix error reporting style in download.c
  contrib/elf2dmp: Fix error reporting style in addrspace.c
  contrib/elf2dmp: Change pa_space_create() signature
  contrib/elf2dmp: Continue even contexts are lacking
  contrib/elf2dmp: Assume error by default
  contrib/elf2dmp: Remove unnecessary err flags
  hw/arm: Deprecate various old Arm machine types

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
pm215 committed Mar 12, 2024
2 parents 05ec974 + 5dd6bfd commit 1c26899
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 232 deletions.
1 change: 1 addition & 0 deletions MAINTAINERS
Original file line number Diff line number Diff line change
Expand Up @@ -3587,6 +3587,7 @@ F: util/iova-tree.c

elf2dmp
M: Viktor Prutyanov <viktor.prutyanov@phystech.edu>
R: Akihiko Odaki <akihiko.odaki@daynix.com>
S: Maintained
F: contrib/elf2dmp/

Expand Down
63 changes: 37 additions & 26 deletions contrib/elf2dmp/addrspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static struct pa_block *pa_space_find_block(struct pa_space *ps, uint64_t pa)
return NULL;
}

static uint8_t *pa_space_resolve(struct pa_space *ps, uint64_t pa)
static void *pa_space_resolve(struct pa_space *ps, uint64_t pa)
{
struct pa_block *block = pa_space_find_block(ps, pa);

Expand All @@ -33,6 +33,19 @@ static uint8_t *pa_space_resolve(struct pa_space *ps, uint64_t pa)
return block->addr + (pa - block->paddr);
}

static bool pa_space_read64(struct pa_space *ps, uint64_t pa, uint64_t *value)
{
uint64_t *resolved = pa_space_resolve(ps, pa);

if (!resolved) {
return false;
}

*value = *resolved;

return true;
}

static void pa_block_align(struct pa_block *b)
{
uint64_t low_align = ((b->paddr - 1) | ELF2DMP_PAGE_MASK) + 1 - b->paddr;
Expand All @@ -57,7 +70,7 @@ static void pa_block_align(struct pa_block *b)
b->paddr += low_align;
}

int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
void pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
{
Elf64_Half phdr_nr = elf_getphdrnum(qemu_elf->map);
Elf64_Phdr *phdr = elf64_getphdr(qemu_elf->map);
Expand All @@ -75,20 +88,19 @@ int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf)
ps->block = g_new(struct pa_block, ps->block_nr);

for (i = 0; i < phdr_nr; i++) {
if (phdr[i].p_type == PT_LOAD) {
if (phdr[i].p_type == PT_LOAD && phdr[i].p_offset < qemu_elf->size) {
ps->block[block_i] = (struct pa_block) {
.addr = (uint8_t *)qemu_elf->map + phdr[i].p_offset,
.paddr = phdr[i].p_paddr,
.size = phdr[i].p_filesz,
.size = MIN(phdr[i].p_filesz,
qemu_elf->size - phdr[i].p_offset),
};
pa_block_align(&ps->block[block_i]);
block_i = ps->block[block_i].size ? (block_i + 1) : block_i;
}
}

ps->block_nr = block_i;

return 0;
}

void pa_space_destroy(struct pa_space *ps)
Expand All @@ -108,19 +120,20 @@ void va_space_create(struct va_space *vs, struct pa_space *ps, uint64_t dtb)
va_space_set_dtb(vs, dtb);
}

static uint64_t get_pml4e(struct va_space *vs, uint64_t va)
static bool get_pml4e(struct va_space *vs, uint64_t va, uint64_t *value)
{
uint64_t pa = (vs->dtb & 0xffffffffff000) | ((va & 0xff8000000000) >> 36);

return *(uint64_t *)pa_space_resolve(vs->ps, pa);
return pa_space_read64(vs->ps, pa, value);
}

static uint64_t get_pdpi(struct va_space *vs, uint64_t va, uint64_t pml4e)
static bool get_pdpi(struct va_space *vs, uint64_t va, uint64_t pml4e,
uint64_t *value)
{
uint64_t pdpte_paddr = (pml4e & 0xffffffffff000) |
((va & 0x7FC0000000) >> 27);

return *(uint64_t *)pa_space_resolve(vs->ps, pdpte_paddr);
return pa_space_read64(vs->ps, pdpte_paddr, value);
}

static uint64_t pde_index(uint64_t va)
Expand All @@ -133,11 +146,12 @@ static uint64_t pdba_base(uint64_t pdpe)
return pdpe & 0xFFFFFFFFFF000;
}

static uint64_t get_pgd(struct va_space *vs, uint64_t va, uint64_t pdpe)
static bool get_pgd(struct va_space *vs, uint64_t va, uint64_t pdpe,
uint64_t *value)
{
uint64_t pgd_entry = pdba_base(pdpe) + pde_index(va) * 8;

return *(uint64_t *)pa_space_resolve(vs->ps, pgd_entry);
return pa_space_read64(vs->ps, pgd_entry, value);
}

static uint64_t pte_index(uint64_t va)
Expand All @@ -150,11 +164,12 @@ static uint64_t ptba_base(uint64_t pde)
return pde & 0xFFFFFFFFFF000;
}

static uint64_t get_pte(struct va_space *vs, uint64_t va, uint64_t pgd)
static bool get_pte(struct va_space *vs, uint64_t va, uint64_t pgd,
uint64_t *value)
{
uint64_t pgd_val = ptba_base(pgd) + pte_index(va) * 8;

return *(uint64_t *)pa_space_resolve(vs->ps, pgd_val);
return pa_space_read64(vs->ps, pgd_val, value);
}

static uint64_t get_paddr(uint64_t va, uint64_t pte)
Expand Down Expand Up @@ -186,31 +201,27 @@ static uint64_t va_space_va2pa(struct va_space *vs, uint64_t va)
{
uint64_t pml4e, pdpe, pgd, pte;

pml4e = get_pml4e(vs, va);
if (!is_present(pml4e)) {
if (!get_pml4e(vs, va, &pml4e) || !is_present(pml4e)) {
return INVALID_PA;
}

pdpe = get_pdpi(vs, va, pml4e);
if (!is_present(pdpe)) {
if (!get_pdpi(vs, va, pml4e, &pdpe) || !is_present(pdpe)) {
return INVALID_PA;
}

if (page_size_flag(pdpe)) {
return get_1GB_paddr(va, pdpe);
}

pgd = get_pgd(vs, va, pdpe);
if (!is_present(pgd)) {
if (!get_pgd(vs, va, pdpe, &pgd) || !is_present(pgd)) {
return INVALID_PA;
}

if (page_size_flag(pgd)) {
return get_2MB_paddr(va, pgd);
}

pte = get_pte(vs, va, pgd);
if (!is_present(pte)) {
if (!get_pte(vs, va, pgd, &pte) || !is_present(pte)) {
return INVALID_PA;
}

Expand All @@ -228,8 +239,8 @@ void *va_space_resolve(struct va_space *vs, uint64_t va)
return pa_space_resolve(vs->ps, pa);
}

int va_space_rw(struct va_space *vs, uint64_t addr,
void *buf, size_t size, int is_write)
bool va_space_rw(struct va_space *vs, uint64_t addr,
void *buf, size_t size, int is_write)
{
while (size) {
uint64_t page = addr & ELF2DMP_PFN_MASK;
Expand All @@ -240,7 +251,7 @@ int va_space_rw(struct va_space *vs, uint64_t addr,

ptr = va_space_resolve(vs, addr);
if (!ptr) {
return 1;
return false;
}

if (is_write) {
Expand All @@ -254,5 +265,5 @@ int va_space_rw(struct va_space *vs, uint64_t addr,
addr += s;
}

return 0;
return true;
}
6 changes: 3 additions & 3 deletions contrib/elf2dmp/addrspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ struct va_space {
struct pa_space *ps;
};

int pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf);
void pa_space_create(struct pa_space *ps, QEMU_Elf *qemu_elf);
void pa_space_destroy(struct pa_space *ps);

void va_space_create(struct va_space *vs, struct pa_space *ps, uint64_t dtb);
void va_space_set_dtb(struct va_space *vs, uint64_t dtb);
void *va_space_resolve(struct va_space *vs, uint64_t va);
int va_space_rw(struct va_space *vs, uint64_t addr,
void *buf, size_t size, int is_write);
bool va_space_rw(struct va_space *vs, uint64_t addr,
void *buf, size_t size, int is_write);

#endif /* ADDRSPACE_H */
12 changes: 5 additions & 7 deletions contrib/elf2dmp/download.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@
#include <curl/curl.h>
#include "download.h"

int download_url(const char *name, const char *url)
bool download_url(const char *name, const char *url)
{
int err = 0;
bool success = false;
FILE *file;
CURL *curl = curl_easy_init();

if (!curl) {
return 1;
return false;
}

file = fopen(name, "wb");
if (!file) {
err = 1;
goto out_curl;
}

Expand All @@ -33,13 +32,12 @@ int download_url(const char *name, const char *url)
|| curl_easy_perform(curl) != CURLE_OK) {
unlink(name);
fclose(file);
err = 1;
} else {
err = fclose(file);
success = !fclose(file);
}

out_curl:
curl_easy_cleanup(curl);

return err;
return success;
}
2 changes: 1 addition & 1 deletion contrib/elf2dmp/download.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
#ifndef DOWNLOAD_H
#define DOWNLOAD_H

int download_url(const char *name, const char *url);
bool download_url(const char *name, const char *url);

#endif /* DOWNLOAD_H */

0 comments on commit 1c26899

Please sign in to comment.