memattrs: add debug attribute
PATCH v7 1/26 memattrs: add debug attribute
Extend the MemTxAttrs to include 'debug' flag. The flag can be used as general indicator that operation was triggered by the debugger.
Later in the patch series we set the debug=1 when issuing a memory access from the gdbstub or HMP commands. This patch is prerequisite to support debugging the encrypted guest. If we see request with debug=1 then we will need to use encryption APIs to access the guest memory.
--- include/exec/memattrs.h | 2 ++ 1 file changed, 2 insertions(+)
include/exec/memattrs.h +2 -0
@@ -37,6 +37,8 @@ typedef struct MemTxAttrs { unsigned int user:1;/* Requester ID (for MSI for example) */ unsigned int requester_id:16; +/* Memory access request from the debugger */ + unsigned int debug:1; } MemTxAttrs;/* Bus masters which don't specify any attributes will get this,
Signature · 1 line
2.14.3
PATCH v7 2/26 exec: add ram_debug_ops support
Currently, the guest memory access for the debug purpose is performed using the memcpy(). Lets extend the 'struct MemoryRegion' to include ram_debug_ops callbacks. The ram_debug_ops can be used to override memcpy() with something else.
The feature can be used by encrypted guest -- which can register callbacks to override memcpy() with memory encryption/decryption APIs.
a typical usage:
mem_read(uint8_t *dst, uint8_t *src, uint32_t len, MemTxAttrs *attrs); mem_write(uint8_t *dst, uint8_t *src, uint32_t len, MemTxAttrs *attrs);
MemoryRegionRAMReadWriteOps ops;
ops.read = mem_read;
ops.write = mem_write;
memory_region_init_ram(mem, NULL, "memory", size, NULL); memory_region_set_ram_debug_ops(mem, ops);
--- exec.c | 66 ++++++++++++++++++++++++++++++++++++++------------- include/exec/memory.h | 28 ++++++++++++++++++++++ 2 files changed, 78 insertions(+), 16 deletions(-)
exec.c +50 -16
@@ -3050,7 +3050,11 @@ static MemTxResult flatview_write_continue(FlatView *fv, hwaddr addr, }else {/* RAM case */ ptr =qemu_ram_ptr_length (mr->ram_block, addr1, &l, false); -memcpy (ptr, buf, l); +if (attrs.debug && mr->ram_debug_ops) { + mr->ram_debug_ops->write (ptr, buf, l, attrs); + }else { +memcpy (ptr, buf, l); + }invalidate_and_set_dirty (mr, addr1, l); }
@@ -3148,7 +3152,11 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, }else {/* RAM case */ ptr =qemu_ram_ptr_length (mr->ram_block, addr1, &l, false); -memcpy (buf, ptr, l); +if (attrs.debug && mr->ram_debug_ops) { + mr->ram_debug_ops->read (buf, ptr, l, attrs); + }else { +memcpy (buf, ptr, l); + } }if (release_lock) {
@@ -3218,11 +3226,13 @@ void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,enum write_rom_type {WRITE_DATA , +READ_DATA ,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 void cpu_physical_memory_rw_internal (AddressSpace *as, +hwaddr addr,uint8_t *buf,int len,MemTxAttrs attrs, +enum write_rom_type type) {hwaddr l;uint8_t *ptr;
@@ -3237,12 +3247,33 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,if (!(memory_region_is_ram (mr) ||memory_region_is_romd (mr))) { l =memory_access_size (mr, l, addr1); +/* Pass MMIO down to address address_space_rw */ +switch (type) { +case READ_DATA : +case WRITE_DATA : +address_space_rw (as, addr1, attrs, buf, l, + type ==WRITE_DATA ); +break ; +case FLUSH_CACHE : +break ; + } }else {/* ROM/RAM case */ ptr =qemu_map_ram_ptr (mr->ram_block, addr1);switch (type) { +case READ_DATA : +if (mr->ram_debug_ops) { + mr->ram_debug_ops->read (buf, ptr, l, attrs); + }else { +memcpy (buf, ptr, l); + } +break ;case WRITE_DATA : -memcpy (ptr, buf, l); +if (mr->ram_debug_ops) { + mr->ram_debug_ops->write (ptr, buf, l, attrs); + }else { +memcpy (ptr, buf, l); + }invalidate_and_set_dirty (mr, addr1, l);break ;case FLUSH_CACHE :
@@ -3261,7 +3292,8 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,void cpu_physical_memory_write_rom (AddressSpace *as,hwaddr addr,const uint8_t *buf,int len) { -cpu_physical_memory_write_rom_internal (as, addr, buf, len,WRITE_DATA ); +cpu_physical_memory_rw_internal (as, addr, (uint8_t *)buf, len, +MEMTXATTRS_UNSPECIFIED ,WRITE_DATA ); }void cpu_flush_icache_range (hwaddr start,int len)
@@ -3276,8 +3308,10 @@ void cpu_flush_icache_range(hwaddr start, int len)return ; } -cpu_physical_memory_write_rom_internal (&address_space_memory, - start,NULL , len,FLUSH_CACHE ); +cpu_physical_memory_rw_internal (&address_space_memory, + start,NULL , len, +MEMTXATTRS_UNSPECIFIED , +FLUSH_CACHE ); } typedef struct {
@@ -3583,6 +3617,7 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,int l;hwaddr phys_addr;target_ulong page; +int type = is_write ?WRITE_DATA :READ_DATA ;cpu_synchronize_state (cpu);while (len >0 ) {
@@ -3592,6 +3627,10 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr, page = addr &TARGET_PAGE_MASK ; phys_addr =cpu_get_phys_page_attrs_debug (cpu, page, &attrs); asidx =cpu_asidx_from_attrs (cpu, attrs); + +/* set debug attrs to indicate memory access is from the debugger */ + attrs.debug =1 ; +/* if no physical page mapped, return an error */ if (phys_addr ==-1 )return -1 ;
@@ -3599,14 +3638,9 @@ int cpu_memory_rw_debug(CPUState *cpu, target_ulong addr,if (l > len) 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); - }else { -address_space_rw (cpu->cpu_ases[asidx].as, phys_addr, -MEMTXATTRS_UNSPECIFIED , - buf, l,0 ); - } +cpu_physical_memory_rw_internal (cpu->cpu_ases[asidx].as, + phys_addr, buf, l, attrs, + type); len -= l; buf += l; addr += l;
include/exec/memory.h +28 -0
@@ -215,6 +215,18 @@ typedef struct IOMMUMemoryRegionClass {typedef struct CoalescedMemoryRange CoalescedMemoryRange ;typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd ; +/* Memory Region RAM debug callback */ +typedef struct MemoryRegionRAMReadWriteOps MemoryRegionRAMReadWriteOps ; + +struct MemoryRegionRAMReadWriteOps { +/* Write data into guest memory */ +int (*write) (uint8_t *dest,const uint8_t *src, +uint32_t len,MemTxAttrs attrs); +/* Read data from guest memory */ +int (*read) (uint8_t *dest,const uint8_t *src, +uint32_t len,MemTxAttrs attrs); +}; +struct MemoryRegion {Object parent_obj;
@@ -254,6 +266,7 @@ struct MemoryRegion {const char *name;unsigned ioeventfd_nb;MemoryRegionIoeventfd *ioeventfds; +const MemoryRegionRAMReadWriteOps *ram_debug_ops; };struct IOMMUMemoryRegion {
@@ -623,6 +636,21 @@ void memory_region_init_rom_device_nomigrate(MemoryRegion *mr,uint64_t size, Error **errp); +/** +* memory_region_set_ram_debug_ops: Set debug access ops for a given memory +* region. +* +* @mr: the #MemoryRegion to be initialized +* @ops: a function that will be used for when accessing @target region during +* debug +*/ +static inline void +memory_region_set_ram_debug_ops (MemoryRegion *mr, +const MemoryRegionRAMReadWriteOps *ops) +{ + mr->ram_debug_ops = ops; +} +/** * memory_region_init_reservation: Initialize a memory region that reserves * I/O space.
Signature · 1 line
2.14.3
Re: [PATCH v7 02/26] exec: add ram_debug_ops support
Quoted 4 lines · Brijesh Singh
Currently, the guest memory access for the debug purpose is performed using the
memcpy(). Lets extend the 'struct MemoryRegion' to includeram_debug_opscallbacks. Theram_debug_opscan be used to overridememcpy()with something else.
[meta-comment]
Your threading is off. This email was sent with the headers:
Quoted 2 lines
In-Reply-To: <[email protected]>
References: <[email protected]>
which ties it only to patch 1/26; in turn, that message has no In-Reply-To header at all, making it a top-level thread. Meanwhile, your 0/26 cover letter has:
Quoted 1 line
Message-Id: <[email protected]>
which nothing in the rest of the series refers to, making it a separate thread. It would be helpful if you could the threading used in your sending environment, although I don't have any specific suggestions on what that fix would be.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v7 02/26] exec: add ram_debug_ops support
Quoted 17 lines · Eric Blake
Quoted 4 lines · Brijesh Singh
Currently, the guest memory access for the debug purpose is performed using the
memcpy(). Lets extend the 'struct MemoryRegion' to includeram_debug_opscallbacks. Theram_debug_opscan be used to overridememcpy()with something else.[meta-comment]
Your threading is off. This email was sent with the headers:
Quoted 2 lines
In-Reply-To: <[email protected]>
References: <[email protected]>which ties it only to patch 1/26; in turn, that message has no In-Reply-To header at all, making it a top-level thread. Meanwhile, your 0/26 cover letter has:
Quoted 7 lines
Quoted 1 line
Message-Id: <[email protected]>
which nothing in the rest of the series refers to, making it a separate thread. It would be helpful if you could the threading used in your sending environment, although I don't have any specific suggestions on what that fix would be.
I am just looking at the my git send email script log and it seems that after sending the cover-letter patch, exchange server timeout and my script restarting send from patch 02/.. hence threading got messed up. sorry about that. If its an issue then I can resend the whole series.
-Brijesh
Re: [PATCH v7 02/26] exec: add ram_debug_ops support
Quoted 9 lines · Brijesh Singh
Quoted 4 lines
[meta-comment]
Your threading is off. This email was sent with the headers:
I am just looking at the my git send email script log and it seems that after sending the cover-letter patch, exchange server timeout and my script restarting send from patch 02/.. hence threading got messed up. sorry about that. If its an issue then I can resend the whole series.
No need to resend for just that issue (we can deal with a one-off, and there may be a v8 anyways depending on what reviewers find). Does your server allow you to pre-specify message ids, or does it always ignore your input and generate its own message ids?
If interruptions are a common issue, you can use 'git send-email --in-reply-to=...' to forcefully (try and) thread a second batch of sent mail to an earlier batch that was interrupted from completion. It may also be possible to have 'git format-patch' pre-thread all mails, although that may already match what you were doing (pre-specifying message ids only works if your server preserves those requested message ids from the input instead of blindly replacing it with its own ids)
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Paolo Bonzini
maintainerRe: [PATCH v7 02/26] exec: add ram_debug_ops support
In reply to Brijesh Singhexec.c MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, · line 3162
Quoted 13 lines · Brijesh Singh
exec.c +5
-1@@ -3148,7 +3152,11 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, }else {/* RAM case */ ptr =qemu_ram_ptr_length (mr->ram_block, addr1, &l, false); -memcpy (buf, ptr, l); +if (attrs.debug && mr->ram_debug_ops) { + mr->ram_debug_ops->read (buf, ptr, l, attrs); + }else { +memcpy (buf, ptr, l); + } }if (release_lock) {
You also need to tweak flatview_read in include/exec/memory.h (probably by adding an "&& !attrs.debug", which leaves the mr->ram_debug_ops->read to the slow path in exec.c).
Quoted 6 lines
exec.c +1
-0@@ -3218,11 +3226,13 @@ void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,enum write_rom_type {WRITE_DATA , +READ_DATA ,FLUSH_CACHE ,
exec.c void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf, · line 3238
Quoted 10 lines
exec.c +3
-2}; -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 void cpu_physical_memory_rw_internal (AddressSpace *as, +hwaddr addr,uint8_t *buf,int len,MemTxAttrs attrs, +enum write_rom_type type) {hwaddr l;uint8_t *ptr;
I wonder if READ_DATA and WRITE_DATA still need to go down to cpu_physical_memory_rw_internal. Maybe you can just call address_space_rw with &address_space_memory as the address space, and "(MemTxAttrs) { .debug = 1 }" as the attributes.
Paolo
Quoted 35 lines
exec.c +22
-1@@ -3237,12 +3247,33 @@ static inline void cpu_physical_memory_write_rom_internal(AddressSpace *as,if (!(memory_region_is_ram (mr) ||memory_region_is_romd (mr))) { l =memory_access_size (mr, l, addr1); +/* Pass MMIO down to address address_space_rw */ +switch (type) { +case READ_DATA : +case WRITE_DATA : +address_space_rw (as, addr1, attrs, buf, l, + type ==WRITE_DATA ); +break ; +case FLUSH_CACHE : +break ; + } }else {/* ROM/RAM case */ ptr =qemu_map_ram_ptr (mr->ram_block, addr1);switch (type) { +case READ_DATA : +if (mr->ram_debug_ops) { + mr->ram_debug_ops->read (buf, ptr, l, attrs); + }else { +memcpy (buf, ptr, l); + } +break ;case WRITE_DATA : -memcpy (ptr, buf, l); +if (mr->ram_debug_ops) { + mr->ram_debug_ops->write (ptr, buf, l, attrs); + }else { +memcpy (ptr, buf, l); + }invalidate_and_set_dirty (mr, addr1, l);break ;case FLUSH_CACHE :
Re: [PATCH v7 02/26] exec: add ram_debug_ops support
Quoted 13 lines · Paolo Bonzini
Quoted 12 lines · Brijesh Singh
exec.c +5
-1@@ -3148,7 +3152,11 @@ MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, }else {/* RAM case */ ptr =qemu_ram_ptr_length (mr->ram_block, addr1, &l, false); -memcpy (buf, ptr, l); +if (attrs.debug && mr->ram_debug_ops) { + mr->ram_debug_ops->read (buf, ptr, l, attrs); + }else { +memcpy (buf, ptr, l); + } }
Quoted 6 lines
exec.c MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, · line 3162
Quoted 1 line
exec.c +0
-0if (release_lock) {You also need to tweak
flatview_readininclude/exec/memory.h(probably by adding an "&& !attrs.debug", which leaves themr->ram_debug_ops->readto the slow path inexec.c).
thanks, I will make the changes in flatview_read to take slow path when debug is enabled.
Quoted 8 lines
Quoted 8 lines
exec.c +1
-0@@ -3218,11 +3226,13 @@ void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf,enum write_rom_type {WRITE_DATA , +READ_DATA ,FLUSH_CACHE , };
Quoted 14 lines
exec.c void cpu_physical_memory_rw(hwaddr addr, uint8_t *buf, · line 3238
Quoted 8 lines
exec.c +3
-2-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 void cpu_physical_memory_rw_internal (AddressSpace *as, +hwaddr addr,uint8_t *buf,int len,MemTxAttrs attrs, +enum write_rom_type type) {hwaddr l;uint8_t *ptr;I wonder if
READ_DATAandWRITE_DATAstill need to go down tocpu_physical_memory_rw_internal. Maybe you can just calladdress_space_rwwith&address_space_memoryas the address space, and "(MemTxAttrs) { .debug = 1 }" as the attributes.
I will take a look to see if I can remove passing down the READ_DATA and WRITE_DATA.
PATCH v7 3/26 exec: add debug version of physical memory read and write API
In reply to Brijesh SinghAdds the following new APIs
- cpu_physical_memory_read_debug
- cpu_physical_memory_write_debug
- cpu_physical_memory_rw_debug
- ldl_phys_debug
- ldq_phys_debug
--- exec.c | 41 +++++++++++++++++++++++++++++++++++++++++ include/exec/cpu-common.h | 15 +++++++++++++++ 2 files changed, 56 insertions(+)
exec.c +41 -0
@@ -3610,6 +3610,47 @@ void address_space_cache_destroy(MemoryRegionCache *cache)#define RCU_READ_UNLOCK () rcu_read_unlock()#include "memory_ldst.inc.c" +uint32_t ldl_phys_debug (CPUState *cpu,hwaddr addr) +{ +MemTxAttrs attrs; +int asidx =cpu_asidx_from_attrs (cpu, attrs); +uint32_t val; + +/* set debug attrs to indicate memory access is from the debugger */ + attrs.debug =1 ; + +cpu_physical_memory_rw_internal (cpu->cpu_ases[asidx].as, + addr, (void *) &val, +4 , attrs,READ_DATA ); +return tswap32 (val); +} + +uint64_t ldq_phys_debug (CPUState *cpu,hwaddr addr) +{ +MemTxAttrs attrs; +int asidx =cpu_asidx_from_attrs (cpu, attrs); +uint64_t val; + +/* set debug attrs to indicate memory access is from the debugger */ + attrs.debug =1 ; + +cpu_physical_memory_rw_internal (cpu->cpu_ases[asidx].as, + addr, (void *) &val, +8 , attrs,READ_DATA ); +return val; +} + +void cpu_physical_memory_rw_debug (hwaddr addr,uint8_t *buf, +int len,int is_write) +{ +MemTxAttrs attrs; + +/* set debug attrs to indicate memory access is from the debugger */ + attrs.debug =1 ; + +address_space_rw (&address_space_memory, addr, attrs, buf, len, is_write); +} +/* virtual memory access for debug (includes writing to ROM) */ int cpu_memory_rw_debug (CPUState *cpu,target_ulong addr,uint8_t *buf,int len,int is_write)
include/exec/cpu-common.h +15 -0
@@ -77,11 +77,26 @@ size_t qemu_ram_pagesize_largest(void);void cpu_physical_memory_rw (hwaddr addr,uint8_t *buf,int len,int is_write); +void cpu_physical_memory_rw_debug (hwaddr addr,uint8_t *buf, +int len,int is_write);static inline void cpu_physical_memory_read (hwaddr addr,void *buf,int len) {cpu_physical_memory_rw (addr, buf, len,0 ); } +static inline void cpu_physical_memory_read_debug (hwaddr addr, +void *buf,int len) +{ +cpu_physical_memory_rw_debug (addr, buf, len,0 ); +} +static inline void cpu_physical_memory_write_debug (hwaddr addr, +const void *buf,int len) +{ +cpu_physical_memory_rw_debug (addr, (void *)buf, len,1 ); +} +uint32_t ldl_phys_debug (CPUState *cpu,hwaddr addr); +uint64_t ldq_phys_debug (CPUState *cpu,hwaddr addr); +static inline void cpu_physical_memory_write (hwaddr addr,const void *buf,int len) {
Signature · 1 line
2.14.3
PATCH v7 4/26 monitor/i386: use debug APIs when accessing guest memory
In reply to Brijesh SinghUpdates HMP commands to use the debug version of APIs when accessing the guest memory.
--- cpus.c | 2 +- disas.c | 2 +- monitor.c | 8 ++++--- target/i386/helper.c | 14 ++++++------ target/i386/monitor.c | 60 +++++++++++++++++++++++++++------------------------ 5 files changed, 46 insertions(+), 40 deletions(-)
cpus.c +1 -1
@@ -2147,7 +2147,7 @@ void qmp_pmemsave(int64_t addr, int64_t size, const char *filename, l =sizeof (buf);if (l > size) l = size; -cpu_physical_memory_read (addr, buf, l); +cpu_physical_memory_read_debug (addr, buf, l);if (fwrite (buf,1 , l, f) != l) {error_setg (errp,QERR_IO_ERROR ); goto exit;
disas.c +1 -1
@@ -586,7 +586,7 @@ static int physical_read_memory(bfd_vma memaddr, bfd_byte *myaddr, int length, struct disassemble_info *info) { -cpu_physical_memory_read (memaddr, myaddr, length); +cpu_physical_memory_read_debug (memaddr, myaddr, length);return 0 ; }
monitor.c +5 -3
@@ -1358,7 +1358,7 @@ static void memory_dump(Monitor *mon, int count, int format, int wsize,if (l > line_size) l = line_size;if (is_physical) { -cpu_physical_memory_read (addr, buf, l); +cpu_physical_memory_read_debug (addr, buf, l); }else {if (cpu_memory_rw_debug (cs, addr, buf, l,0 ) <0 ) {monitor_printf (mon," Cannot access memory\n" );
@@ -1564,8 +1564,10 @@ static void hmp_sum(Monitor *mon, const QDict *qdict) sum =0 ;for (addr = start; addr < (start + size); addr++) { -uint8_t val =address_space_ldub (&address_space_memory, addr, -MEMTXATTRS_UNSPECIFIED ,NULL ); +uint8_t buf[0 ]; +uint8_t val; +cpu_physical_memory_read_debug (addr, buf,1 ); + val =ldub_p (buf);/* BSD sum algorithm ('sum' Unix command) */ sum = (sum >>1 ) | (sum <<15 ); sum += val;
target/i386/helper.c +7 -7
@@ -757,7 +757,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)if (la57) { pml5e_addr = ((env->cr[3 ] & ~0xfff ) + (((addr >>48 ) &0x1ff ) <<3 )) & a20_mask; - pml5e =x86_ldq_phys (cs, pml5e_addr); + pml5e =ldq_phys_debug (cs, pml5e_addr);if (!(pml5e &PG_PRESENT_MASK )) {return -1 ; }
@@ -767,7 +767,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) pml4e_addr = ((pml5e &PG_ADDRESS_MASK ) + (((addr >>39 ) &0x1ff ) <<3 )) & a20_mask; - pml4e =x86_ldq_phys (cs, pml4e_addr); + pml4e =ldq_phys_debug (cs, pml4e_addr);if (!(pml4e &PG_PRESENT_MASK )) {return -1 ; }
@@ -788,14 +788,14 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) { pdpe_addr = ((env->cr[3 ] & ~0x1f ) + ((addr >>27 ) &0x18 )) & a20_mask; - pdpe =x86_ldq_phys (cs, pdpe_addr); + pdpe =ldq_phys_debug (cs, pdpe_addr);if (!(pdpe &PG_PRESENT_MASK ))return -1 ; } pde_addr = ((pdpe &PG_ADDRESS_MASK ) + (((addr >>21 ) &0x1ff ) <<3 )) & a20_mask; - pde =x86_ldq_phys (cs, pde_addr); + pde =ldq_phys_debug (cs, pde_addr);if (!(pde &PG_PRESENT_MASK )) {return -1 ; }
@@ -808,7 +808,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) pte_addr = ((pde &PG_ADDRESS_MASK ) + (((addr >>12 ) &0x1ff ) <<3 )) & a20_mask; page_size =4096 ; - pte =x86_ldq_phys (cs, pte_addr); + pte =ldq_phys_debug (cs, pte_addr); }if (!(pte &PG_PRESENT_MASK )) {return -1 ;
@@ -818,7 +818,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)/* page directory entry */ pde_addr = ((env->cr[3 ] & ~0xfff ) + ((addr >>20 ) &0xffc )) & a20_mask; - pde =x86_ldl_phys (cs, pde_addr); + pde =ldl_phys_debug (cs, pde_addr);if (!(pde &PG_PRESENT_MASK ))return -1 ;if ((pde &PG_PSE_MASK ) && (env->cr[4 ] &CR4_PSE_MASK )) {
@@ -827,7 +827,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) }else {/* page directory entry */ pte_addr = ((pde & ~0xfff ) + ((addr >>10 ) &0xffc )) & a20_mask; - pte =x86_ldl_phys (cs, pte_addr); + pte =ldl_phys_debug (cs, pte_addr);if (!(pte &PG_PRESENT_MASK )) {return -1 ; }
target/i386/monitor.c +32 -28
200 lines not on this page · raw message
Signature · 1 line
2.14.3
Paolo Bonzini
maintainerRe: [PATCH v7 04/26] monitor/i386: use debug APIs when accessing guest memory
monitor.c static void hmp_sum(Monitor *mon, const QDict *qdict) · line 1570
Quoted 9 lines · Brijesh Singh
sum = 0; for(addr = start; addr < (start + size); addr++) {monitor.c +4
-2-uint8_t val =address_space_ldub (&address_space_memory, addr, -MEMTXATTRS_UNSPECIFIED ,NULL ); +uint8_t buf[0 ]; +uint8_t val; +cpu_physical_memory_read_debug (addr, buf,1 ); + val =ldub_p (buf);
This is a buffer overflow on "buf". All you need is
cpu_physical_memory_read_debug(addr, &val, 1);
Thanks,
Paolo
Quoted 2 lines
/* BSD sum algorithm ('sum' Unix command) */
sum = (sum >> 1) | (sum << 15);PATCH v7 5/26 target/i386: add memory encryption feature cpuid support
In reply to Brijesh SinghAMD EPYC processors support memory encryption feature. The feature is reported through CPUID 8000_001F[EAX].
Fn8000_001F [EAX]:
Bit 0 Secure Memory Encryption (SME) supported Bit 1 Secure Encrypted Virtualization (SEV) supported Bit 2 Page flush MSR supported Bit 3 Ecrypted State (SEV-ES) support
when memory encryption feature is reported, CPUID 8000_001F[EBX] should provide additional information regarding the feature (such as which page table bit is used to mark pages as encrypted etc). The information in EBX and ECX may vary from one family to another hence we use the host cpuid to populate the EBX information.
The details for memory encryption CPUID is available in AMD APM
(https://support.amd.com/TechDocs/24594.pdf) Section E.4.17
--- target/i386/cpu.c | 36 ++++++++++++++++++++++++++++++++++++ target/i386/cpu.h | 6 ++++++ 2 files changed, 42 insertions(+)
target/i386/cpu.c +36 -0
@@ -234,6 +234,7 @@ static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_EXT4_FEATURES 0#define TCG_SVM_FEATURES 0#define TCG_KVM_FEATURES 0 +#define TCG_MEM_ENCRYPT_FEATURES 0#define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP | \ CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX | \ CPUID_7_0_EBX_PCOMMIT | CPUID_7_0_EBX_CLFLUSHOPT | \
@@ -545,6 +546,20 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { .cpuid_reg = R_EDX, .tcg_features = ~0U, }, + [FEAT_MEM_ENCRYPT] = { + .feat_names = { + "sme", "sev", "page-flush-msr", "sev-es", + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + }, + .cpuid_eax = 0x8000001F, .cpuid_reg = R_EAX, + .tcg_features = TCG_MEM_ENCRYPT_FEATURES, + } }; typedef struct X86RegisterInfo32 {
@@ -1965,6 +1980,9 @@ static X86CPUDefinition builtin_x86_defs[] = { CPUID_XSAVE_XGETBV1, .features[FEAT_6_EAX ] = CPUID_6_EAX_ARAT, +/* Missing: SEV_ES */ + .features[FEAT_MEM_ENCRYPT ] = + CPUID_8000_001F_EAX_SME | CPUID_8000_001F_EAX_SEV, .xlevel = 0x8000000A, .model_id = "AMD EPYC Processor", },
@@ -3589,6 +3607,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx =0 ; }break ; +case 0x8000001F : +if (env->features[FEAT_MEM_ENCRYPT ] &CPUID_8000_001F_EAX_SEV ) { + *eax = env->features[FEAT_MEM_ENCRYPT ]; +host_cpuid (0x8000001F ,0 ,NULL , ebx,NULL ,NULL ); + *ecx =0 ; + *edx =0 ; + }else { + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; + } +break ;case 0xC0000000 : *eax = env->cpuid_xlevel2; *ebx =0 ;
@@ -4036,10 +4067,15 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)x86_cpu_adjust_feat_level (cpu,FEAT_C000_0001_EDX );x86_cpu_adjust_feat_level (cpu,FEAT_SVM );x86_cpu_adjust_feat_level (cpu,FEAT_XSAVE ); +x86_cpu_adjust_feat_level (cpu,FEAT_MEM_ENCRYPT );/* SVM requires CPUID[0x8000000A] */ if (env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_SVM ) {x86_cpu_adjust_level (cpu, &env->cpuid_min_xlevel,0x8000000A ); } +/* SEV requires CPUID[0x8000001F] */ +if ((env->features[FEAT_MEM_ENCRYPT ] &CPUID_8000_001F_EAX_SEV )) { +x86_cpu_adjust_level (cpu, &env->cpuid_min_xlevel,0x8000001F ); + } }/* Set cpuid_*level* based on cpuid_min_*level, if not explicitly set */
target/i386/cpu.h +6 -0
@@ -483,6 +483,7 @@ typedef enum FeatureWord {FEAT_6_EAX ,/* CPUID[6].EAX */ FEAT_XSAVE_COMP_LO ,/* CPUID[EAX=0xd,ECX=0].EAX */ FEAT_XSAVE_COMP_HI ,/* CPUID[EAX=0xd,ECX=0].EDX */ +FEAT_MEM_ENCRYPT ,/* CPUID[8000_001F].EAX */ FEATURE_WORDS , } FeatureWord;
@@ -679,6 +680,11 @@ typedef uint32_t FeatureWordArray[FEATURE_WORDS];#define CPUID_6_EAX_ARAT (1U << 2) +#define CPUID_8000_001F_EAX_SME (1U << 0)/* SME */ +#define CPUID_8000_001F_EAX_SEV (1U << 1)/* SEV */ +#define CPUID_8000_001F_EAX_PAGE_FLUSH_MSR (1U << 2)/* Page flush MSR */ +#define CPUID_8000_001F_EAX_SEV_ES (1U << 3)/* SEV-ES */ +/* CPUID[0x80000007].EDX flags: */ #define CPUID_APM_INVTSC (1U << 8)
Signature · 1 line
2.14.3
Paolo Bonzini
maintainerRe: [PATCH v7 05/26] target/i386: add memory encryption feature cpuid support
Quoted 40 lines · Brijesh Singh
AMD EPYC processors support memory encryption feature. The feature is reported through CPUID 8000_001F[EAX].
Fn8000_001F[EAX]:Bit 0 Secure Memory Encryption (SME) supported Bit 1 Secure Encrypted Virtualization (SEV) supported Bit 2 Page flush MSR supported Bit 3 Ecrypted State (SEV-ES) supportwhen memory encryption feature is reported, CPUID 8000_001F[EBX] should provide additional information regarding the feature (such as which page table bit is used to mark pages as encrypted etc). The information in EBX and ECX may vary from one family to another hence we use the host cpuid to populate the EBX information.
The details for memory encryption CPUID is available in AMD APM
(https://support.amd.com/TechDocs/24594.pdf) Section E.4.17Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Eduardo Habkost <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- target/i386/cpu.c | 36 ++++++++++++++++++++++++++++++++++++ target/i386/cpu.h | 6 ++++++ 2 files changed, 42 insertions(+)target/i386/cpu.c +1
-0@@ -234,6 +234,7 @@ static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_EXT4_FEATURES 0#define TCG_SVM_FEATURES 0#define TCG_KVM_FEATURES 0 +#define TCG_MEM_ENCRYPT_FEATURES 0#define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP | \ CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX | \ CPUID_7_0_EBX_PCOMMIT | CPUID_7_0_EBX_CLFLUSHOPT | \
target/i386/cpu.c static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { · line 551
Quoted 6 lines
target/i386/cpu.c +3
-0@@ -545,6 +546,20 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { .cpuid_reg = R_EDX, .tcg_features = ~0U, }, + [FEAT_MEM_ENCRYPT] = { + .feat_names = { + "sme", "sev", "page-flush-msr", "sev-es",
Why would sme ever be on in a guest? That is, would a guest care about whether SME is on in the host?
Paolo
Quoted 85 lines
target/i386/cpu.c +32
-0+ NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + NULL, NULL, NULL, NULL, + }, + .cpuid_eax = 0x8000001F, .cpuid_reg = R_EAX, + .tcg_features = TCG_MEM_ENCRYPT_FEATURES, + } }; typedef struct X86RegisterInfo32 {@@ -1965,6 +1980,9 @@ static X86CPUDefinition builtin_x86_defs[] = { CPUID_XSAVE_XGETBV1, .features[FEAT_6_EAX ] = CPUID_6_EAX_ARAT, +/* Missing: SEV_ES */ + .features[FEAT_MEM_ENCRYPT ] = + CPUID_8000_001F_EAX_SME | CPUID_8000_001F_EAX_SEV, .xlevel = 0x8000000A, .model_id = "AMD EPYC Processor", },@@ -3589,6 +3607,19 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count, *edx =0 ; }break ; +case 0x8000001F : +if (env->features[FEAT_MEM_ENCRYPT ] &CPUID_8000_001F_EAX_SEV ) { + *eax = env->features[FEAT_MEM_ENCRYPT ]; +host_cpuid (0x8000001F ,0 ,NULL , ebx,NULL ,NULL ); + *ecx =0 ; + *edx =0 ; + }else { + *eax =0 ; + *ebx =0 ; + *ecx =0 ; + *edx =0 ; + } +break ;case 0xC0000000 : *eax = env->cpuid_xlevel2; *ebx =0 ;@@ -4036,10 +4067,15 @@ static void x86_cpu_expand_features(X86CPU *cpu, Error **errp)x86_cpu_adjust_feat_level (cpu,FEAT_C000_0001_EDX );x86_cpu_adjust_feat_level (cpu,FEAT_SVM );x86_cpu_adjust_feat_level (cpu,FEAT_XSAVE ); +x86_cpu_adjust_feat_level (cpu,FEAT_MEM_ENCRYPT );/* SVM requires CPUID[0x8000000A] */ if (env->features[FEAT_8000_0001_ECX ] &CPUID_EXT3_SVM ) {x86_cpu_adjust_level (cpu, &env->cpuid_min_xlevel,0x8000000A ); } +/* SEV requires CPUID[0x8000001F] */ +if ((env->features[FEAT_MEM_ENCRYPT ] &CPUID_8000_001F_EAX_SEV )) { +x86_cpu_adjust_level (cpu, &env->cpuid_min_xlevel,0x8000001F ); + } }/* Set cpuid_*level* based on cpuid_min_*level, if not explicitly set */ target/i386/cpu.h +6
-0@@ -483,6 +483,7 @@ typedef enum FeatureWord {FEAT_6_EAX ,/* CPUID[6].EAX */ FEAT_XSAVE_COMP_LO ,/* CPUID[EAX=0xd,ECX=0].EAX */ FEAT_XSAVE_COMP_HI ,/* CPUID[EAX=0xd,ECX=0].EDX */ +FEAT_MEM_ENCRYPT ,/* CPUID[8000_001F].EAX */ FEATURE_WORDS , } FeatureWord;@@ -679,6 +680,11 @@ typedef uint32_t FeatureWordArray[FEATURE_WORDS];#define CPUID_6_EAX_ARAT (1U << 2) +#define CPUID_8000_001F_EAX_SME (1U << 0)/* SME */ +#define CPUID_8000_001F_EAX_SEV (1U << 1)/* SEV */ +#define CPUID_8000_001F_EAX_PAGE_FLUSH_MSR (1U << 2)/* Page flush MSR */ +#define CPUID_8000_001F_EAX_SEV_ES (1U << 3)/* SEV-ES */ +/* CPUID[0x80000007].EDX flags: */ #define CPUID_APM_INVTSC (1U << 8)
Re: [PATCH v7 05/26] target/i386: add memory encryption feature cpuid support
Quoted 42 lines · Paolo Bonzini
Quoted 41 lines · Brijesh Singh
AMD EPYC processors support memory encryption feature. The feature is reported through CPUID 8000_001F[EAX].
Fn8000_001F[EAX]:Bit 0 Secure Memory Encryption (SME) supported Bit 1 Secure Encrypted Virtualization (SEV) supported Bit 2 Page flush MSR supported Bit 3 Ecrypted State (SEV-ES) supportwhen memory encryption feature is reported, CPUID 8000_001F[EBX] should provide additional information regarding the feature (such as which page table bit is used to mark pages as encrypted etc). The information in EBX and ECX may vary from one family to another hence we use the host cpuid to populate the EBX information.
The details for memory encryption CPUID is available in AMD APM
(https://support.amd.com/TechDocs/24594.pdf) Section E.4.17Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Eduardo Habkost <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- target/i386/cpu.c | 36 ++++++++++++++++++++++++++++++++++++ target/i386/cpu.h | 6 ++++++ 2 files changed, 42 insertions(+)target/i386/cpu.c +1
-0@@ -234,6 +234,7 @@ static void x86_cpu_vendor_words2str(char *dst, uint32_t vendor1,#define TCG_EXT4_FEATURES 0#define TCG_SVM_FEATURES 0#define TCG_KVM_FEATURES 0 +#define TCG_MEM_ENCRYPT_FEATURES 0#define TCG_7_0_EBX_FEATURES (CPUID_7_0_EBX_SMEP | CPUID_7_0_EBX_SMAP | \ CPUID_7_0_EBX_BMI1 | CPUID_7_0_EBX_BMI2 | CPUID_7_0_EBX_ADX | \ CPUID_7_0_EBX_PCOMMIT | CPUID_7_0_EBX_CLFLUSHOPT | \@@ -545,6 +546,20 @@ static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { .cpuid_reg = R_EDX,
Quoted 8 lines
target/i386/cpu.c static FeatureWordInfo feature_word_info[FEATURE_WORDS] = { · line 551
Quoted 5 lines
target/i386/cpu.c +3
-0.tcg_features = ~0U, }, + [FEAT_MEM_ENCRYPT] = { + .feat_names = { + "sme", "sev", "page-flush-msr", "sev-es",Why would sme ever be on in a guest? That is, would a guest care about whether SME is on in the host?
Guest does not need the SME feature bit exposed and it does not care whether SME is enabled on the host. Since I was defining the bit fields from 8000_001F EAX hence I tried to list them all. In next version, I will update the MEM_ENCRYPT feature to expose just SEV.
Thanks
PATCH v7 6/26 machine: add -memory-encryption property
In reply to Brijesh SinghWhen CPU supports memory encryption feature, the property can be used to specify the encryption object to use when launching an encrypted guest.
--- hw/core/machine.c | 22 ++++++++++++++++++++++ include/hw/boards.h | 1 + qemu-options.hx | 2 ++ 3 files changed, 25 insertions(+)
hw/core/machine.c +22 -0
@@ -334,6 +334,22 @@ static bool machine_get_enforce_config_section(Object *obj, Error **errp)return ms->enforce_config_section; } +static char *machine_get_memory_encryption (Object *obj,Error **errp) +{ +MachineState *ms =MACHINE (obj); + +return g_strdup (ms->memory_encryption); +} + +static void machine_set_memory_encryption (Object *obj,const char *value, +Error **errp) +{ +MachineState *ms =MACHINE (obj); + +g_free (ms->memory_encryption); + ms->memory_encryption =g_strdup (value); +} +void machine_class_allow_dynamic_sysbus_dev (MachineClass *mc,const char *type) {strList *item =g_new0 (strList,1 );
@@ -612,6 +628,12 @@ static void machine_class_init(ObjectClass *oc, void *data) &error_abort);object_class_property_set_description (oc,"enforce-config-section" ,"Set on to enforce configuration section migration" , &error_abort); + +object_class_property_add_str (oc,"memory-encryption" , + machine_get_memory_encryption, machine_set_memory_encryption, + &error_abort); +object_class_property_set_description (oc,"memory-encryption" , +"Set memory encyption object to use" , &error_abort); }static void machine_class_base_init (ObjectClass *oc,void *data)
include/hw/boards.h +1 -0
@@ -243,6 +243,7 @@ struct MachineState {bool suppress_vmdesc;bool enforce_config_section;bool enable_graphics; +char *memory_encryption;ram_addr_t ram_size;ram_addr_t maxram_size;
qemu-options.hx +2 -0
@@ -110,6 +110,8 @@ code to send configuration section even if the machine-type sets the @option{migration.send-configuration} property to @var{off}. NOTE: this parameter is deprecated. Please use @option{-global} @option{migration.send-configuration}=@var{on|off} instead. +@item memory-encryption=@var{} +Memory encryption object to use. The default is none. @end table ETEXI
Signature · 1 line
2.14.3
PATCH v7 7/26 kvm: update kvm.h to include memory encryption ioctls
In reply to Brijesh SinghUpdates kmv.h to include memory encryption ioctls and SEV commands.
--- linux-headers/linux/kvm.h | 90 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+)
linux-headers/linux/kvm.h +90 -0
@@ -1362,6 +1362,96 @@ struct kvm_s390_ucas_mapping {/* Available with KVM_CAP_S390_CMMA_MIGRATION */ #define KVM_S390_GET_CMMA_BITS _IOWR(KVMIO, 0xb8, struct kvm_s390_cmma_log)#define KVM_S390_SET_CMMA_BITS _IOW(KVMIO, 0xb9, struct kvm_s390_cmma_log) +/* Memory Encryption Commands */ +#define KVM_MEMORY_ENCRYPT_OP _IOWR(KVMIO, 0xba, unsigned long) + +struct kvm_enc_region { +__u64 addr; +__u64 size; +}; + +#define KVM_MEMORY_ENCRYPT_REG_REGION _IOR(KVMIO, 0xbb, struct kvm_enc_region) +#define KVM_MEMORY_ENCRYPT_UNREG_REGION _IOR(KVMIO, 0xbc, struct kvm_enc_region) + +/* Secure Encrypted Virtualization command */ +enum sev_cmd_id { +/* Guest initialization commands */ +KVM_SEV_INIT =0 , +KVM_SEV_ES_INIT , +/* Guest launch commands */ +KVM_SEV_LAUNCH_START , +KVM_SEV_LAUNCH_UPDATE_DATA , +KVM_SEV_LAUNCH_UPDATE_VMSA , +KVM_SEV_LAUNCH_SECRET , +KVM_SEV_LAUNCH_MEASURE , +KVM_SEV_LAUNCH_FINISH , +/* Guest migration commands (outgoing) */ +KVM_SEV_SEND_START , +KVM_SEV_SEND_UPDATE_DATA , +KVM_SEV_SEND_UPDATE_VMSA , +KVM_SEV_SEND_FINISH , +/* Guest migration commands (incoming) */ +KVM_SEV_RECEIVE_START , +KVM_SEV_RECEIVE_UPDATE_DATA , +KVM_SEV_RECEIVE_UPDATE_VMSA , +KVM_SEV_RECEIVE_FINISH , +/* Guest status and debug commands */ +KVM_SEV_GUEST_STATUS , +KVM_SEV_DBG_DECRYPT , +KVM_SEV_DBG_ENCRYPT , +/* Guest certificates commands */ +KVM_SEV_CERT_EXPORT , + +KVM_SEV_NR_MAX , +}; + +struct kvm_sev_cmd { +__u32 id; +__u64 data; +__u32 error; +__u32 sev_fd; +}; + +struct kvm_sev_launch_start { +__u32 handle; +__u32 policy; +__u64 dh_uaddr; +__u32 dh_len; +__u64 session_uaddr; +__u32 session_len; +}; + +struct kvm_sev_launch_update_data { +__u64 uaddr; +__u32 len; +}; + + +struct kvm_sev_launch_secret { +__u64 hdr_uaddr; +__u32 hdr_len; +__u64 guest_uaddr; +__u32 guest_len; +__u64 trans_uaddr; +__u32 trans_len; +}; + +struct kvm_sev_launch_measure { +__u64 uaddr; +__u32 len; +}; + +struct kvm_sev_guest_status { +__u32 handle; +__u32 policy; +__u32 state; +}; + +struct kvm_sev_dbg { +__u64 src_uaddr; +__u64 dst_uaddr; +__u32 len; +};#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
Signature · 1 line
2.14.3
PATCH v7 8/26 docs: add AMD Secure Encrypted Virtualization (SEV)
In reply to Brijesh SinghCreate a documentation entry to describe the AMD Secure Encrypted Virtualization (SEV) feature.
--- docs/amd-memory-encryption.txt | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 docs/amd-memory-encryption.txt
docs/amd-memory-encryption.txt new file +92 -0
@@ -0,0 +1,92 @@ +Secure Encrypted Virtualization (SEV) is a feature found on AMD processors. + +SEV is an extension to the AMD-V architecture which supports running encrypted +virtual machine (VMs) under the control of KVM. Encrypted VMs have their pages +(code and data) secured such that only the guest itself has access to the +unencrypted version. Each encrypted VM is associated with a unique encryption +key; if its data is accessed to a different entity using a different key the +encrypted guests data will be incorrectly decrypted, leading to unintelligible +data. + +The key management of this feature is handled by separate processor known as +AMD secure processor (AMD-SP) which is present in AMD SOCs. Firmware running +inside the AMD-SP provide commands to support common VM lifecycle. This +includes commands for launching, snapshotting, migrating and debugging the +encrypted guest. Those SEV command can be issued via KVM_MEMORY_ENCRYPT_OP +ioctls. + +Launching +--------- +Boot images (such as bios) must be encrypted before guest can be booted. +MEMORY_ENCRYPT_OP ioctl provides commands to encrypt the images :LAUNCH_START, +LAUNCH_UPDATE_DATA, LAUNCH_MEASURE and LAUNCH_FINISH. These four commands +together generate a fresh memory encryption key for the VM, encrypt the boot +images and provide a measurement than can be used as an attestation of the +successful launch. + +LAUNCH_START is called first to create a cryptographic launch context within +the firmware. To create this context, guest owner must provides guest policy, +its public Diffie-Hellman key (PDH) and session parameters. These inputs +should be treated as binary blob and must be passed as-is to the SEV firmware. + +The guest policy is passed as plaintext and hypervisor may able to read it +but should not modify it (any modification of the policy bits will result +in bad measurement). The guest policy is a 4-byte data structure containing +several flags that restricts what can be done on running SEV guest. +See KM Spec section 3 and 6.2 for more details. + +Guest owners provided DH certificate and session parameters will be used to +establish a cryptographic session with the guest owner to negotiate keys used +for the attestation. + +LAUNCH_UPDATE_DATA encrypts the memory region using the cryptographic context +created via LAUNCH_START command. If required, this command can be called +multiple times to encrypt different memory regions. The command also calculates +the measurement of the memory contents as it encrypts. + +LAUNCH_MEASURE command can be used to retrieve the measurement of encrypted +memory. This measurement is a signature of the memory contents that can be +sent to the guest owner as an attestation that the memory was encrypted +correctly by the firmware. The guest owner may wait to provide the guest +confidential information until it can verify the attestation measurement. +Since the guest owner knows the initial contents of the guest at boot, the +attestation measurement can be verified by comparing it to what the guest owner +expects. + +LAUNCH_FINISH command finalizes the guest launch and destroy's the cryptographic +context. + +See SEV KM API Spec [1] 'Launching a guest' usage flow (Appendix A) for the +complete flow chart. + +Debugging +----------- +Since memory contents of SEV guest is encrypted hence hypervisor access to the +guest memory will get a cipher text. If guest policy allows debugging, then +hypervisor can use DEBUG_DECRYPT and DEBUG_ENCRYPT commands access the guest +memory region for debug purposes. + +Snapshot/Restore +----------------- +TODO + +Live Migration +---------------- +TODO + +References +----------------- + +AMD Memory Encryption whitepaper: +http://amd-dev.wpengine.netdna-cdn.com/wordpress/media/2013/12/AMD_Memory_Encryption_Whitepaper_v7-Public.pdf + +Secure Encrypted Virutualization Key Management: +[1] http://support.amd.com/TechDocs/55766_SEV-KM API_Specification.pdf + +KVM Forum slides: +http://www.linux-kvm.org/images/7/74/02x08A-Thomas_Lendacky-AMDs_Virtualizatoin_Memory_Encryption_Technology.pdf + +AMD64 Architecture Programmer's Manual: + http://support.amd.com/TechDocs/24593.pdf + SME is section 7.10 + SEV is section 15.34
Signature · 1 line
2.14.3
PATCH v7 9/26 accel: add Secure Encrypted Virtulization (SEV) object
In reply to Brijesh SinghAdd a new memory encryption object 'sev-guest'. The object will be used to create enrypted VMs on AMD EPYC CPU. The object provides the properties to pass guest owner's public Diffie-hellman key, guest policy and session information required to create the memory encryption context within the SEV firmware.
e.g to launch SEV guest
# $QEMU \
-object sev-guest,id=sev0 \
-machine ....,memory-encryption=sev0--- accel/kvm/Makefile.objs | 2 +- accel/kvm/sev.c | 214 +++++++++++++++++++++++++++++++++++++++++ docs/amd-memory-encryption.txt | 17 ++++ include/sysemu/sev.h | 54 +++++++++++ qemu-options.hx | 36 +++++++ 5 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 accel/kvm/sev.c create mode 100644 include/sysemu/sev.h
accel/kvm/Makefile.objs +1 -1
@@ -1 +1 @@ -obj-$(CONFIG_KVM) += kvm-all.o +obj-$(CONFIG_KVM) += kvm-all.o sev.o
accel/kvm/sev.c new file +214 -0
214 lines not on this page · raw message
docs/amd-memory-encryption.txt +17 -0
@@ -35,10 +35,21 @@ in bad measurement). The guest policy is a 4-byte data structure containing several flags that restricts what can be done on running SEV guest. See KM Spec section 3 and 6.2 for more details. +The guest policy can be provided via the 'policy' property (see below) + +# ${QEMU} \ + sev-guest,id=sev0,policy=0x1...\ + Guest owners provided DH certificate and session parameters will be used to establish a cryptographic session with the guest owner to negotiate keys used for the attestation. +The DH certificate and session blob can be provided via 'dh-cert-file' and +'session-file' property (see below + +# ${QEMU} \ + sev-guest,id=sev0,dh-cert-file=<file1>,session-file=<file2> + LAUNCH_UPDATE_DATA encrypts the memory region using the cryptographic context created via LAUNCH_START command. If required, this command can be called multiple times to encrypt different memory regions. The command also calculates
@@ -59,6 +70,12 @@ context. See SEV KM API Spec [1] 'Launching a guest' usage flow (Appendix A) for the complete flow chart. +To launch a SEV guest + +# ${QEMU} \ + -machine ...,memory-encryption=sev0 \ + -object sev-guest,id=sev0 + Debugging ----------- Since memory contents of SEV guest is encrypted hence hypervisor access to the
include/sysemu/sev.h new file +54 -0
54 lines not on this page · raw message
qemu-options.hx +36 -0
@@ -4282,6 +4282,42 @@ contents of @code{iv.b64} to the second secret data=$SECRET,iv=$(<iv.b64) @end example +@item -object sev-guest,id=@var{id},sev-device=@var{string}[cbitpos=@var{cbitpos},policy=@var{policy},handle=@var{handle},dh-cert-file=@var{file},session-file=@var{file}] + +Create a Secure Encrypted Virtualization (SEV) guest object, which can be used +to provide the guest memory encryption support on AMD processors. + +The @option{sev-device} provides the device file to use for communicating with +the SEV firmware running inside AMD Secure Processor. The default device is +'/dev/sev'. If hardware supports memory encryption then /dev/sev devices are +created by CCP driver. + +The @option{cbitpos} provide the C-bit location in guest page table entry to use. + +The @option{policy} provides the guest policy to be enforced by the SEV firmware +and restrict what configuration and operational commands can be performed on this +guest by the hypervisor. The policy should be provided by the guest owner and is +bound to the guest and cannot be changed throughout the lifetime of the guest. +The default is 0. + +If guest @option{policy} allows sharing the key with another SEV guest then +@option{handle} can be use to provide handle of the guest from which to share +the key. + +The @option{dh-cert-file} and @option{session-file} provides the guest owner's +Public Diffie-Hillman key defined in SEV spec. The PDH and session parameters +are used for establishing a cryptographic session with the guest owner to +negotiate keys used for attestation. The file must be encoded in base64. + +e.g to launch a SEV guest +@example + # $QEMU \ + ...... + -object sev-guest,id=sev0 \ + -machine ...,memory-encryption=sev0 + ..... + +@end example @end table ETEXI
Signature · 1 line
2.14.3
PATCH v7 10/26 sev: add command to initialize the memory encryption context
In reply to Brijesh SinghWhen memory encryption is enabled, KVM_SEV_INIT command is used to initialize the platform. The command loads the SEV related persistent data from non-volatile storage and initializes the platform context. This command should be first issued before invoking any other guest commands provided by the SEV firmware.
--- accel/kvm/kvm-all.c | 15 +++++ accel/kvm/sev.c | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ accel/kvm/trace-events | 2 + accel/stubs/kvm-stub.c | 28 +++++++++ include/sysemu/sev.h | 16 +++++ 5 files changed, 222 insertions(+)
accel/kvm/kvm-all.c +15 -0
@@ -38,6 +38,7 @@#include "qemu/event_notifier.h" #include "trace.h" #include "hw/irq.h" +#include "sysemu/sev.h" #include "hw/boards.h"
@@ -103,6 +104,9 @@ struct KVMState#endif KVMMemoryListener memory_listener; QLIST_HEAD(, KVMParkedVcpu) kvm_parked_vcpus; + +/* memory encryption */ +void *memcrypt_handle; };KVMState *kvm_state;
@@ -1632,6 +1636,17 @@ static int kvm_init(MachineState *ms) kvm_state = s; +/* +* if memory encryption object is specified then initialize the memory +* encryption context. +*/ +if (ms->memory_encryption) { + kvm_state->memcrypt_handle =sev_guest_init (ms->memory_encryption); +if (!kvm_state->memcrypt_handle) { + goto err; + } + } + ret =kvm_arch_init (ms, s);if (ret <0 ) { goto err;
accel/kvm/sev.c +161 -0
177 lines not on this page · raw message
accel/kvm/trace-events +2 -0
@@ -13,3 +13,5 @@ kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s vector %d vi kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d" kvm_irqchip_release_virq(int virq) "virq %d" +# sev.c +kvm_sev_init(void) ""
accel/stubs/kvm-stub.c +28 -0
@@ -14,6 +14,7 @@#include "qemu-common.h" #include "cpu.h" #include "sysemu/kvm.h" +#include "sysemu/sev.h" #ifndef CONFIG_USER_ONLY #include "hw/pci/msi.h"
@@ -33,6 +34,11 @@ bool kvm_readonly_mem_allowed;bool kvm_ioeventfd_any_length_allowed;bool kvm_msi_use_devid; +bool sev_allowed; +uint8_t sev_fw_major; +uint8_t sev_fw_minor; +uint8_t sev_fw_build; +int kvm_destroy_vcpu (CPUState *cpu) {return -ENOSYS ;
@@ -105,6 +111,28 @@ int kvm_on_sigbus(int code, void *addr)return 1 ; } +void sev_get_current_state (char **state) +{ +} + +bool sev_enabled (void ) +{ +return false; +} + +uint64_t sev_get_me_mask (void ) +{ +return ~0UL ; +} + +void sev_get_fw_version (uint8_t *major,uint8_t *minor,uint8_t *build) +{ +} + +void sev_get_policy (uint32_t *policy) +{ +} +#ifndef CONFIG_USER_ONLY int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev) {
include/sysemu/sev.h +16 -0
@@ -14,6 +14,8 @@#ifndef QEMU_SEV_H #define QEMU_SEV_H +#include <linux/kvm.h> +#include "qom/object.h" #include "qapi/error.h" #include "sysemu/kvm.h"
@@ -23,6 +25,12 @@#define QSEV_GUEST_INFO (obj) \ OBJECT_CHECK(QSevGuestInfo, (obj), TYPE_QSEV_GUEST_INFO) +extern bool sev_enabled (void ); +extern uint64_t sev_get_me_mask (void ); +extern void sev_get_current_state (char **state); +extern void sev_get_fw_version (uint8_t *major,uint8_t *minor,uint8_t *build); +extern void sev_get_policy (uint32_t *policy); +typedef struct QSevGuestInfo QSevGuestInfo ;typedef struct QSevGuestInfoClass QSevGuestInfoClass ;
@@ -50,5 +58,13 @@ struct QSevGuestInfoClass { ObjectClass parent_class; }; +struct SEVState { +QSevGuestInfo *sev_info; +}; + +typedef struct SEVState SEVState ; + +void *sev_guest_init (const char *id); +#endif
Signature · 1 line
2.14.3
PATCH v7 11/26 sev: register the guest memory range which may contain encrypted data
In reply to Brijesh SinghWhen SEV is enabled, the hardware encryption engine uses a tweak such that the two identical plaintext at different location will have a different ciphertexts. So swapping or moving a ciphertexts of two guest pages will not result in plaintexts being swapped. Hence relocating a physical backing pages of the SEV guest will require some additional steps in KVM driver. The KVM_MEMORY_ENCRYPT_{UN,}REG_REGION ioctl can be used to register/unregister the guest memory region which may contain the encrypted data. KVM driver will internally handle the relocating physical backing pages of registered memory regions.
--- accel/kvm/sev.c | 41 +++++++++++++++++++++++++++++++++++++++++ accel/kvm/trace-events | 2 ++ 2 files changed, 43 insertions(+)
accel/kvm/sev.c +41 -0
@@ -86,6 +86,45 @@ fw_error_to_str(int code)return sev_fw_errlist[code]; } +static void +sev_ram_block_added (RAMBlockNotifier *n,void *host,size_t size) +{ +int r; +struct kvm_enc_region range; + + range.addr = (__u64 )host; + range.size = size; + +trace_kvm_memcrypt_register_region (host, size); + r =kvm_vm_ioctl (kvm_state,KVM_MEMORY_ENCRYPT_REG_REGION , &range); +if (r) { +error_report ("%s: failed to register region (%p+%#lx)" , + __func__, host, size); + } +} + +static void +sev_ram_block_removed (RAMBlockNotifier *n,void *host,size_t size) +{ +int r; +struct kvm_enc_region range; + + range.addr = (__u64 )host; + range.size = size; + +trace_kvm_memcrypt_unregister_region (host, size); + r =kvm_vm_ioctl (kvm_state,KVM_MEMORY_ENCRYPT_UNREG_REGION , &range); +if (r) { +error_report ("%s: failed to unregister region (%p+%#lx)" , + __func__, host, size); + } +} + +static struct RAMBlockNotifier sev_ram_notifier = { + .ram_block_added = sev_ram_block_added, + .ram_block_removed = sev_ram_block_removed, +}; +static void qsev_guest_finalize (Object *obj) {
@@ -360,6 +399,8 @@ sev_guest_init(const char *id) } sev_active = true; +ram_block_notifier_add (&sev_ram_notifier); +return s; err:g_free (s);
accel/kvm/trace-events +2 -0
@@ -15,3 +15,5 @@ kvm_irqchip_release_virq(int virq) "virq %d" # sev.c kvm_sev_init(void) "" +kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%lu" +kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu"
Signature · 1 line
2.14.3
PATCH v7 12/26 kvm: introduce memory encryption APIs
In reply to Brijesh SinghInorder to integerate the Secure Encryption Virtualization (SEV) support add few high-level memory encryption APIs which can be used for encrypting the guest memory region.
--- accel/kvm/kvm-all.c | 30 ++++++++++++++++++++++++++++++ accel/stubs/kvm-stub.c | 14 ++++++++++++++ include/sysemu/kvm.h | 25 +++++++++++++++++++++++++ 3 files changed, 69 insertions(+)
accel/kvm/kvm-all.c +30 -0
@@ -107,6 +107,8 @@ struct KVMState/* memory encryption */ void *memcrypt_handle; +int (*memcrypt_encrypt_data)(void *handle,uint8_t *ptr,uint64_t len); +void (*memcrypt_debug_ops)(void *handle,MemoryRegion *mr); };KVMState *kvm_state;
@@ -142,6 +144,34 @@ int kvm_get_max_memslots(void)return s->nr_slots; } +bool kvm_memcrypt_enabled (void ) +{ +if (kvm_state && kvm_state->memcrypt_handle) { +return true; + } + +return false; +} + +int kvm_memcrypt_encrypt_data (uint8_t *ptr,uint64_t len) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_encrypt_data) { +return kvm_state->memcrypt_encrypt_data (kvm_state->memcrypt_handle, + ptr, len); + } + +return 1 ; +} + +void kvm_memcrypt_set_debug_ops (MemoryRegion *mr) +{ +if (kvm_state->memcrypt_handle && + kvm_state->memcrypt_debug_ops) { + kvm_state->memcrypt_debug_ops (kvm_state->memcrypt_handle, mr); + } +} +static KVMSlot *kvm_get_free_slot (KVMMemoryListener *kml) {KVMState *s = kvm_state;
accel/stubs/kvm-stub.c +14 -0
@@ -133,6 +133,20 @@ void sev_get_policy(uint32_t *policy) { } +bool kvm_memcrypt_enabled (void ) +{ +return false; +} + +int kvm_memcrypt_encrypt_data (uint8_t *ptr,uint64_t len) +{ +return 1 ; +} + +void kvm_memcrypt_set_debug_ops (MemoryRegion *mr) +{ +} +#ifndef CONFIG_USER_ONLY int kvm_irqchip_add_msi_route (KVMState *s,int vector,PCIDevice *dev) {
include/sysemu/kvm.h +25 -0
@@ -231,6 +231,31 @@ int kvm_destroy_vcpu(CPUState *cpu);*/ bool kvm_arm_supports_user_irq (void ); +/** +* kvm_memcrypt_enabled - return boolean indicating whether memory encryption +* is enabled +* Returns: 1 memory encryption is enabled +* 0 memory encryption is disabled +*/ +bool kvm_memcrypt_enabled (void ); + +/** +* kvm_memcrypt_encrypt_data: encrypt the memory range +* +* Return: 1 failed to encrypt the range +* 0 succesfully encrypted memory region +*/ +int kvm_memcrypt_encrypt_data (uint8_t *ptr,uint64_t len); + +/** +* kvm_memcrypt_set_debug_ram_ops: set debug_ram_ops callback +* +* When debug_ram_ops is set, debug access to this memory region will use +* memory encryption APIs. +*/ +void kvm_memcrypt_set_debug_ops (MemoryRegion *mr); + +#ifdef NEED_CPU_H #include "cpu.h"
Signature · 1 line
2.14.3
PATCH v7 13/26 qmp: add query-sev command
In reply to Brijesh SinghThe QMP query command can used to retrieve the SEV information when memory encryption is enabled on AMD platform.
--- qapi-schema.json | 47 +++++++++++++++++++++++++++++++++++++++++++++++ qmp.c | 16 ++++++++++++++++ 2 files changed, 63 insertions(+)
qapi-schema.json +47 -0
@@ -3189,3 +3189,50 @@ # Since: 2.11 ## { 'command': 'watchdog-set-action', 'data' : {'action': 'WatchdogAction'} } + +## +# @SevInfo: +# +# Information about SEV support +# +# @enabled: true if SEV is active +# +# @api_major: SEV API major version +# +# @api_minor: SEV API minor version +# +# @build_id: SEV FW build id +# +# @policy: SEV policy value +# +# @state: SEV guest state +# +# Since: 2.12 +## +{ 'struct': 'SevInfo', + 'data': { 'enabled': 'bool', + 'api_major': 'uint8', + 'api_minor' : 'uint8', + 'build_id' : 'uint8', + 'policy' : 'uint32', + 'state' : 'str' + } +} + +## +# @query-sev: +# +# Returns information about SEV +# +# Returns: @SevInfo +# +# Since: 2.12 +# +# Example: +# +# -> { "execute": "query-sev" } +# <- { "return": { "enabled": true, "api-major" : 0, "api-minor" : 0, +# "build-id" : 0, "policy" : 0, "state" : "running" } } +# +## +{ 'command': 'query-sev', 'returns': 'SevInfo' }
qmp.c +16 -0
@@ -37,6 +37,7 @@#include "qom/object_interfaces.h" #include "hw/mem/pc-dimm.h" #include "hw/acpi/acpi_dev_interface.h" +#include "sysemu/sev.h" NameInfo *qmp_query_name (Error **errp) {
@@ -717,3 +718,18 @@ MemoryInfo *qmp_query_memory_size_summary(Error **errp)return mem_info; } + +SevInfo *qmp_query_sev (Error **errp) +{ +SevInfo *info =g_malloc0 (sizeof (*info)); + + info->enabled =sev_enabled (); +if (info->enabled) { +sev_get_fw_version (&info->api_major, + &info->api_minor, &info->build_id); +sev_get_policy (&info->policy); +sev_get_current_state (&info->state); + } + +return info; +}
Signature · 1 line
2.14.3
Re: [PATCH v7 13/26] qmp: add query-sev command
Quoted 9 lines · Brijesh Singh
The QMP query command can used to retrieve the SEV information when memory encryption is enabled on AMD platform.
Cc "Daniel P. Berrangé" <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Cc Markus Armbruster <[email protected]>Signed-off-by Brijesh Singh <[email protected]>---
qapi-schema.json line 3200
Quoted 8 lines
qapi-schema.json +8
-0+## +# @SevInfo: +# +# Information about SEV support +# +# @enabled: true if SEV is active +# +# @api_major: SEV API major version
New QMP structures should prefer '-' over '_'; this should be 'api-major'.
qapi-schema.json line 3204
Quoted 4 lines
qapi-schema.json +4
-0+# +# @api_minor: SEV API minor version +# +# @build_id: SEV FW build id
and so on.
Quoted 11 lines
qapi-schema.json +11
-0+# +# @policy: SEV policy value +# +# @state: SEV guest state +# +# Since: 2.12 +## +{ 'struct': 'SevInfo', + 'data': { 'enabled': 'bool', + 'api_major': 'uint8', + 'api_minor' : 'uint8',
qapi-schema.json line 3225
Quoted 10 lines
qapi-schema.json +10
-0+ 'build_id' : 'uint8', + 'policy' : 'uint32', + 'state' : 'str' + } +} + +## +# @query-sev: +# +# Returns information about SEV
It would be worth expanding the acronym here, for someone that is reading the documentation but not familiar with the term.
qapi-schema.json line 3235
Quoted 10 lines
qapi-schema.json +10
-0+# +# Returns: @SevInfo +# +# Since: 2.12 +# +# Example: +# +# -> { "execute": "query-sev" } +# <- { "return": { "enabled": true, "api-major" : 0, "api-minor" : 0, +# "build-id" : 0, "policy" : 0, "state" : "running" } }
This example does not match the spelling you used in the struct above, but DOES match what the spelling should be.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v7 13/26] qmp: add query-sev command
Quoted 7 lines · Eric Blake
qapi-schema.json line 3200
Quoted 4 lines
qapi-schema.json +4
-0+# +# @enabled: true if SEV is active +# +# @api_major: SEV API major versionNew QMP structures should prefer '-' over '_'; this should be 'api-major'.
I will fix it. thanks
....
Quoted 8 lines
qapi-schema.json line 3225
Quoted 4 lines
qapi-schema.json +4
-0+## +# @query-sev: +# +# Returns information about SEVIt would be worth expanding the acronym here, for someone that is reading the documentation but not familiar with the term.
Will do.
PATCH v7 14/26 hmp: add 'info sev' command
In reply to Brijesh SinghThe command can be used to show the SEV information when memory encryption is enabled on AMD platform.
--- hmp-commands-info.hx | 14 ++++++++++++++ hmp.c | 19 +++++++++++++++++++ hmp.h | 1 + 3 files changed, 34 insertions(+)
hmp-commands-info.hx +14 -0
@@ -865,6 +865,20 @@ STEXI @findex info memory_size_summary Display the amount of initially allocated and present hotpluggable (if enabled) memory in bytes. +ETEXI + + { + .name = "sev", + .args_type = "", + .params = "", + .help = "show SEV information", + .cmd = hmp_info_sev, + }, + +STEXI +@item info sev +@findex info sev +Show SEV information. ETEXI STEXI
hmp.c +19 -0
@@ -2922,3 +2922,22 @@ void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict) }hmp_handle_error (mon, &err); } + +void hmp_info_sev (Monitor *mon,const QDict *qdict) +{ +SevInfo *info; + + info =qmp_query_sev (NULL ); +monitor_printf (mon,"sev support: " ); +monitor_printf (mon,"%s\n" , info->enabled ?"enabled" :"disabled" ); + +if (info->enabled) { +monitor_printf (mon,"state: %s\n" , info->state); +monitor_printf (mon,"policy: 0x%x\n" , info->policy); +monitor_printf (mon,"build id: %u\n" , info->build_id); +monitor_printf (mon,"api version: %u.%u\n" , + info->api_major, info->api_minor); + } + +qapi_free_SevInfo (info); +}
hmp.h +1 -0
@@ -148,5 +148,6 @@ void hmp_info_ramblock(Monitor *mon, const QDict *qdict);void hmp_hotpluggable_cpus (Monitor *mon,const QDict *qdict);void hmp_info_vm_generation_id (Monitor *mon,const QDict *qdict);void hmp_info_memory_size_summary (Monitor *mon,const QDict *qdict); +void hmp_info_sev (Monitor *mon,const QDict *qdict);#endif
Signature · 1 line
2.14.3
PATCH v7 15/26 sev: add command to create launch memory encryption context
In reply to Brijesh SinghThe KVM_SEV_LAUNCH_START command creates a new VM encryption key (VEK). The encryption key created with the command will be used for encrypting the bootstrap images (such as guest bios).
--- accel/kvm/sev.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ accel/kvm/trace-events | 2 + include/sysemu/sev.h | 10 +++++ 3 files changed, 111 insertions(+)
accel/kvm/sev.c +99 -0
@@ -29,6 +29,17 @@ static int sev_fd;#define SEV_FW_MAX_ERROR 0x17 +static SevGuestState current_sev_guest_state =SEV_STATE_UNINIT ; + +static const char *const sev_state_str[] = { +"uninit" , +"lupdate" , +"secret" , +"running" , +"supdate" , +"rupdate" , +}; +static const char *const sev_fw_errlist[] = {"" ,"Platform state is invalid" ,
@@ -86,6 +97,16 @@ fw_error_to_str(int code)return sev_fw_errlist[code]; } +static void +sev_set_guest_state (SevGuestState new_state) +{ +assert (new_state <SEV_STATE_MAX ); + +trace_kvm_sev_change_state (sev_state_str[current_sev_guest_state], + sev_state_str[new_state]); + current_sev_guest_state = new_state; +} +static void sev_ram_block_added (RAMBlockNotifier *n,void *host,size_t size) {
@@ -337,6 +358,7 @@ sev_get_me_mask(void)void sev_get_current_state (char **state) { + *state =g_strdup (sev_state_str[current_sev_guest_state]); }bool
@@ -355,6 +377,76 @@ sev_get_policy(uint32_t *policy) { } +static int +sev_read_file_base64 (const char *filename,guchar **data,gsize *len) +{ +gsize sz; +gchar *base64; +GError *error =NULL ; + +if (!g_file_get_contents (filename, &base64, &sz, &error)) { +error_report ("failed to read '%s' (%s)" , filename, error->message); +return -1 ; + } + + *data =g_base64_decode (base64, len); +return 0 ; +} + +static int +sev_launch_start (SEVState *s) +{ +gsize sz; +int ret =1 ; +int fw_error; +QSevGuestInfo *sev = s->sev_info; +struct kvm_sev_launch_start *start; +guchar *session =NULL , *dh_cert =NULL ; + + start =g_malloc0 (sizeof (*start)); +if (!start) { +return 1 ; + } + + start->handle =object_property_get_int (OBJECT (sev),"handle" , + &error_abort); + start->policy =object_property_get_int (OBJECT (sev),"policy" , + &error_abort); +if (sev->session_file) { +if (sev_read_file_base64 (sev->session_file, &session, &sz) <0 ) { +return 1 ; + } + start->session_uaddr = (unsigned long )session; + start->session_len = sz; + } + +if (sev->dh_cert_file) { +if (sev_read_file_base64 (sev->dh_cert_file, &dh_cert, &sz) <0 ) { +return 1 ; + } + start->dh_uaddr = (unsigned long )dh_cert; + start->dh_len = sz; + } + +trace_kvm_sev_launch_start (start->policy, session, dh_cert); + ret =sev_ioctl (KVM_SEV_LAUNCH_START , start, &fw_error); +if (ret <0 ) { +error_report ("%s: LAUNCH_START ret=%d fw_error=%d '%s'" , + __func__, ret, fw_error,fw_error_to_str (fw_error)); +return 1 ; + } + +object_property_set_int (OBJECT (sev), start->handle,"handle" , + &error_abort); +sev_set_guest_state (SEV_STATE_LUPDATE ); + +g_free (start); +g_free (session); +g_free (dh_cert); + +return 0 ; +} +void *sev_guest_init (const char *id) {
@@ -398,6 +490,13 @@ sev_guest_init(const char *id) goto err; } + ret =sev_launch_start (s); +if (ret) { +error_report ("%s: failed to create encryption context" , __func__); + goto err; + } + + sev_active = true;ram_block_notifier_add (&sev_ram_notifier);
accel/kvm/trace-events +2 -0
@@ -17,3 +17,5 @@ kvm_irqchip_release_virq(int virq) "virq %d" kvm_sev_init(void) "" kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%lu" kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu" +kvm_sev_change_state(const char *old, const char *new) "%s -> %s" +kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p"
include/sysemu/sev.h +10 -0
@@ -58,6 +58,16 @@ struct QSevGuestInfoClass {ObjectClass parent_class; }; +typedef enum { +SEV_STATE_UNINIT =0 , +SEV_STATE_LUPDATE , +SEV_STATE_SECRET , +SEV_STATE_RUNNING , +SEV_STATE_SUPDATE , +SEV_STATE_RUPDATE , +SEV_STATE_MAX +}SevGuestState ; +struct SEVState {QSevGuestInfo *sev_info; };
Signature · 1 line
2.14.3
PATCH v7 16/26 sev: add command to encrypt guest memory region
In reply to Brijesh SinghThe KVM_SEV_LAUNCH_UPDATE_DATA command is used to encrypt a guest memory region using the VM Encryption Key created using LAUNCH_START.
--- accel/kvm/kvm-all.c | 2 ++ accel/kvm/sev.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ accel/kvm/trace-events | 1 + include/sysemu/sev.h | 1 + 4 files changed, 53 insertions(+)
accel/kvm/kvm-all.c +2 -0
@@ -1675,6 +1675,8 @@ static int kvm_init(MachineState *ms)if (!kvm_state->memcrypt_handle) { goto err; } + + kvm_state->memcrypt_encrypt_data = sev_encrypt_data; } ret =kvm_arch_init (ms, s);
accel/kvm/sev.c +49 -0
67 lines not on this page · raw message
accel/kvm/trace-events +1 -0
@@ -19,3 +19,4 @@ kvm_memcrypt_register_region(void *addr, size_t len) "addr %p len 0x%lu" kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu" kvm_sev_change_state(const char *old, const char *new) "%s -> %s" kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p" +kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64
include/sysemu/sev.h +1 -0
@@ -75,6 +75,7 @@ struct SEVState {typedef struct SEVState SEVState ;void *sev_guest_init (const char *id); +int sev_encrypt_data (void *handle,uint8_t *ptr,uint64_t len);#endif
Signature · 1 line
2.14.3
PATCH v7 17/26 target/i386: encrypt bios rom
In reply to Brijesh SinghSEV requires that guest bios must be encrypted before booting the guest.
--- hw/i386/pc_sysfw.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
hw/i386/pc_sysfw.c +13 -0
@@ -112,6 +112,8 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)pflash_t *system_flash;MemoryRegion *flash_mem;char name[64 ]; +void *flash_ptr; +int ret, flash_size; sector_bits =12 ; sector_size =1 << sector_bits;
@@ -168,6 +170,17 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)if (unit ==0 ) { flash_mem =pflash_cfi01_get_memory (system_flash);pc_isa_bios_init (rom_memory, flash_mem, size); + +/* Encrypt the pflash boot ROM */ +if (kvm_memcrypt_enabled ()) { + flash_ptr =memory_region_get_ram_ptr (flash_mem); + flash_size =memory_region_size (flash_mem); + ret =kvm_memcrypt_encrypt_data (flash_ptr, flash_size); +if (ret) { +error_report ("failed to encrypt pflash rom" ); +exit (1 ); + } + } } } }
Signature · 1 line
2.14.3
Re: [PATCH v7 17/26] target/i386: encrypt bios rom
Quoted 1 line · * Brijesh Singh ([email protected]) wrote:
SEV requires that guest bios must be encrypted before booting the guest.
I'm curious; is it just the main BIOS that needs encryption - what about things like device/PXE rom images?
Dave
Quoted 44 lines
Cc "Michael S. Tsirkin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Eduardo Habkost <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- hw/i386/pc_sysfw.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)hw/i386/pc_sysfw.c +13
-0@@ -112,6 +112,8 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)pflash_t *system_flash;MemoryRegion *flash_mem;char name[64 ]; +void *flash_ptr; +int ret, flash_size; sector_bits =12 ; sector_size =1 << sector_bits;@@ -168,6 +170,17 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)if (unit ==0 ) { flash_mem =pflash_cfi01_get_memory (system_flash);pc_isa_bios_init (rom_memory, flash_mem, size); + +/* Encrypt the pflash boot ROM */ +if (kvm_memcrypt_enabled ()) { + flash_ptr =memory_region_get_ram_ptr (flash_mem); + flash_size =memory_region_size (flash_mem); + ret =kvm_memcrypt_encrypt_data (flash_ptr, flash_size); +if (ret) { +error_report ("failed to encrypt pflash rom" ); +exit (1 ); + } + } } } }Signature · 1 line
2.14.3
--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
Re: [PATCH v7 17/26] target/i386: encrypt bios rom
Quoted 4 lines · Dr. David Alan Gilbert
Quoted 1 line · * Brijesh Singh ([email protected]) wrote:
SEV requires that guest bios must be encrypted before booting the guest.
I'm curious; is it just the main BIOS that needs encryption - what about things like device/PXE rom images?
SEV feature is available in OVMF BIOS only. EDKII core contains UEFI firmware driver for several things including PXE and most of time we don't need ROMs. If we do, I think typically ROMs from which the guest firmware reads the binaries are not guest RAM, they are MMIO. Guest BIOSes copies the ROM from MMIO to guest RAM -- it will get encrypted during copy (because MMIO is mapped with C=0 and guest RAM is mapped with C=1). In other words, I don't see any need for encrypting the ROM images during the launch flow. But passing an arbitrary option ROM can be security concerns hence I believe a guest owner wanting to pass a option ROM will use secure boot, then option roms can be verified before executing.
Quoted 47 lines
Dave
Quoted 43 lines
Cc "Michael S. Tsirkin" <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Richard Henderson <[email protected]>Cc Eduardo Habkost <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- hw/i386/pc_sysfw.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)hw/i386/pc_sysfw.c +13
-0@@ -112,6 +112,8 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)pflash_t *system_flash;MemoryRegion *flash_mem;char name[64 ]; +void *flash_ptr; +int ret, flash_size; sector_bits =12 ; sector_size =1 << sector_bits;@@ -168,6 +170,17 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)if (unit ==0 ) { flash_mem =pflash_cfi01_get_memory (system_flash);pc_isa_bios_init (rom_memory, flash_mem, size); + +/* Encrypt the pflash boot ROM */ +if (kvm_memcrypt_enabled ()) { + flash_ptr =memory_region_get_ram_ptr (flash_mem); + flash_size =memory_region_size (flash_mem); + ret =kvm_memcrypt_encrypt_data (flash_ptr, flash_size); +if (ret) { +error_report ("failed to encrypt pflash rom" ); +exit (1 ); + } + } } } }Signature · 1 line
2.14.3--
Dr. David Alan Gilbert / [email protected] / Manchester, UK
PATCH v7 18/26 sev: add support to LAUNCH_MEASURE command
In reply to Brijesh SinghDuring machine creation we encrypted the guest bios image, the LAUNCH_MEASURE command can be used to retrieve the measurement of the encrypted memory region. This measurement is a signature of the memory contents that can be sent to the guest owner as an attestation that the memory was encrypted correctly by the firmware. VM management tools like libvirt can query the measurement using query-launch-measure QMP command.
--- accel/kvm/sev.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ accel/kvm/trace-events | 1 + accel/stubs/kvm-stub.c | 5 ++++ include/sysemu/sev.h | 2 ++ 4 files changed, 75 insertions(+)
accel/kvm/sev.c +67 -0
@@ -19,6 +19,7 @@#include "sysemu/sev.h" #include "sysemu/sysemu.h" #include "trace.h" +#include "qapi-event.h" #define DEFAULT_GUEST_POLICY 0x1/* disable debug */ #define DEFAULT_SEV_DEVICE "/dev/sev"
@@ -26,6 +27,7 @@static uint64_t me_mask;static bool sev_active;static int sev_fd; +static SEVState *sev_state;#define SEV_FW_MAX_ERROR 0x17
@@ -483,6 +485,68 @@ err:return ret; } +static void +sev_launch_get_measure (Notifier *notifier,void *unused) +{ +int ret, error; +guchar *data; +SEVState *s = sev_state; +struct kvm_sev_launch_measure *measurement; + +if (!sev_check_state (SEV_STATE_LUPDATE )) { +return ; + } + + measurement =g_malloc0 (sizeof (*measurement)); +if (!measurement) { +return ; + } + +/* query the measurement blob length */ + ret =sev_ioctl (KVM_SEV_LAUNCH_MEASURE , measurement, &error); +if (!measurement->len) { +error_report ("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'" , + __func__, ret, error,fw_error_to_str (errno)); + goto free_measurement; + } + + data =g_malloc (measurement->len); +if (s->measurement) { + goto free_data; + } + + measurement->uaddr = (unsigned long )data; + +/* get the measurement blob */ + ret =sev_ioctl (KVM_SEV_LAUNCH_MEASURE , measurement, &error); +if (ret) { +error_report ("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'" , + __func__, ret, error,fw_error_to_str (errno)); + goto free_data; + } + +sev_set_guest_state (SEV_STATE_SECRET ); + +/* encode the measurement value and emit the event */ + s->measurement =g_base64_encode (data, measurement->len); +trace_kvm_sev_launch_measurement (s->measurement); + +free_data: +g_free (data); +free_measurement: +g_free (measurement); +} + +char * +sev_get_launch_measurement (void ) +{ +return g_strdup (sev_state->measurement); +} + +static Notifier sev_machine_done_notify = { + .notify = sev_launch_get_measure, +}; +void *sev_guest_init (const char *id) {
@@ -535,6 +599,9 @@ sev_guest_init(const char *id) sev_active = true;ram_block_notifier_add (&sev_ram_notifier); +qemu_add_machine_init_done_notifier (&sev_machine_done_notify); + + sev_state = s;return s; err:
accel/kvm/trace-events +1 -0
@@ -20,3 +20,4 @@ kvm_memcrypt_unregister_region(void *addr, size_t len) "addr %p len 0x%lu" kvm_sev_change_state(const char *old, const char *new) "%s -> %s" kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p" kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64 +kvm_sev_launch_measurement(const char *value) "data %s"
accel/stubs/kvm-stub.c +5 -0
@@ -133,6 +133,11 @@ void sev_get_policy(uint32_t *policy) { } +char *sev_get_launch_measurement (void ) +{ +return NULL ; +} +bool kvm_memcrypt_enabled (void ) {return false;
include/sysemu/sev.h +2 -0
@@ -30,6 +30,7 @@ extern uint64_t sev_get_me_mask(void);extern void sev_get_current_state (char **state);extern void sev_get_fw_version (uint8_t *major,uint8_t *minor,uint8_t *build);extern void sev_get_policy (uint32_t *policy); +extern char *sev_get_launch_measurement (void );typedef struct QSevGuestInfo QSevGuestInfo ;typedef struct QSevGuestInfoClass QSevGuestInfoClass ;
@@ -70,6 +71,7 @@ typedef enum {struct SEVState {QSevGuestInfo *sev_info; +gchar *measurement; };typedef struct SEVState SEVState ;
Signature · 1 line
2.14.3
PATCH v7 19/26 sev: Finalize the SEV guest launch flow
In reply to Brijesh SinghSEV launch flow requires us to issue LAUNCH_FINISH command before guest is ready to run.
--- accel/kvm/sev.c | 29 +++++++++++++++++++++++++++++ accel/kvm/trace-events | 1 + 2 files changed, 30 insertions(+)
accel/kvm/sev.c +29 -0
@@ -547,6 +547,34 @@ static Notifier sev_machine_done_notify = { .notify = sev_launch_get_measure, }; +static void +sev_launch_finish (SEVState *s) +{ +int ret, error; + +trace_kvm_sev_launch_finish (); + ret =sev_ioctl (KVM_SEV_LAUNCH_FINISH ,0 , &error); +if (ret) { +error_report ("%s: LAUNCH_FINISH ret=%d fw_error=%d '%s'" , + __func__, ret, error,fw_error_to_str (error)); +exit (1 ); + } + +sev_set_guest_state (SEV_STATE_RUNNING ); +} + +static void +sev_vm_state_change (void *opaque,int running,RunState state) +{ +SEVState *s = opaque; + +if (running) { +if (!sev_check_state (SEV_STATE_RUNNING )) { +sev_launch_finish (s); + } + } +} +void *sev_guest_init (const char *id) {
@@ -600,6 +628,7 @@ sev_guest_init(const char *id) sev_active = true;ram_block_notifier_add (&sev_ram_notifier);qemu_add_machine_init_done_notifier (&sev_machine_done_notify); +qemu_add_vm_change_state_handler (sev_vm_state_change, s); sev_state = s;
accel/kvm/trace-events +1 -0
@@ -21,3 +21,4 @@ kvm_sev_change_state(const char *old, const char *new) "%s -> %s" kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session %p pdh %p" kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64 kvm_sev_launch_measurement(const char *value) "data %s" +kvm_sev_launch_finish(void) ""
Signature · 1 line
2.14.3
PATCH v7 20/26 hw: i386: set ram_debug_ops when memory encryption is enabled
In reply to Brijesh SinghWhen memory encryption is enabled, the guest RAM and boot flash ROM will contain the encrypted data. By setting the debug ops allow us to invoke encryption APIs when accessing the memory for the debug purposes.
--- hw/i386/pc.c | 9 +++++++++ hw/i386/pc_sysfw.c | 6 ++++++ 2 files changed, 15 insertions(+)
hw/i386/pc.c +9 -0
@@ -1357,6 +1357,15 @@ void pc_memory_init(PCMachineState *pcms,e820_add_entry (0x100000000ULL , pcms->above_4g_mem_size,E820_RAM ); } +/* +* When memory encryption is enabled, the guest RAM will be encrypted with +* a guest unique key. Set the debug ops so that any debug access to the +* guest RAM will go through the memory encryption APIs. +*/ +if (kvm_memcrypt_enabled ()) { +kvm_memcrypt_set_debug_ops (ram); + } +if (!pcmc->has_reserved_memory && (machine->ram_slots || (machine->maxram_size > machine->ram_size))) {
hw/i386/pc_sysfw.c +6 -0
@@ -180,6 +180,12 @@ static void pc_system_flash_init(MemoryRegion *rom_memory)error_report ("failed to encrypt pflash rom" );exit (1 ); } + +/* +* The pflash ROM is encrypted, set the debug ops so that any +* debug accesses will use memory encryption APIs. +*/ +kvm_memcrypt_set_debug_ops (flash_mem); } } }
Signature · 1 line
2.14.3
PATCH v7 21/26 sev: add debug encrypt and decrypt commands
In reply to Brijesh SinghKVM_SEV_DBG_DECRYPT and KVM_SEV_DBG_ENCRYPT commands are used for decrypting and encrypting guest memory region. The command works only if the guest policy allows the debugging.
--- accel/kvm/kvm-all.c | 1 + accel/kvm/sev.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++ accel/kvm/trace-events | 1 + include/sysemu/sev.h | 1 + 4 files changed, 75 insertions(+)
accel/kvm/kvm-all.c +1 -0
@@ -1677,6 +1677,7 @@ static int kvm_init(MachineState *ms) } kvm_state->memcrypt_encrypt_data = sev_encrypt_data; + kvm_state->memcrypt_debug_ops = sev_set_debug_ops; } ret =kvm_arch_init (ms, s);
accel/kvm/sev.c +72 -0
95 lines not on this page · raw message
accel/kvm/trace-events +1 -0
@@ -22,3 +22,4 @@ kvm_sev_launch_start(int policy, void *session, void *pdh) "policy 0x%x session kvm_sev_launch_update_data(void *addr, uint64_t len) "addr %p len 0x%" PRIu64 kvm_sev_launch_measurement(const char *value) "data %s" kvm_sev_launch_finish(void) "" +kvm_sev_debug(const char *op, const uint8_t *src, uint8_t *dst, int len) "(%s) src %p dst %p len %d"
include/sysemu/sev.h +1 -0
@@ -78,6 +78,7 @@ typedef struct SEVState SEVState;void *sev_guest_init (const char *id);int sev_encrypt_data (void *handle,uint8_t *ptr,uint64_t len); +void sev_set_debug_ops (void *handle,MemoryRegion *mr);#endif
Signature · 1 line
2.14.3
PATCH v7 22/26 target/i386: clear C-bit when walking SEV guest page table
In reply to Brijesh SinghIn SEV-enabled guest the pte entry will have C-bit set, we need to clear the C-bit when walking the page table.
--- target/i386/helper.c | 31 +++++++++++++---------- target/i386/monitor.c | 69 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 63 insertions(+), 37 deletions(-)
target/i386/helper.c +18 -13
@@ -21,6 +21,7 @@#include "cpu.h" #include "exec/exec-all.h" #include "sysemu/kvm.h" +#include "sysemu/sev.h" #include "kvm_i386.h" #ifndef CONFIG_USER_ONLY #include "sysemu/sysemu.h"
@@ -732,6 +733,9 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)int32_t a20_mask;uint32_t page_offset;int page_size; +uint64_t me_mask; + + me_mask =sev_get_me_mask (); a20_mask =x86_get_a20_mask (env);if (!(env->cr[0 ] &CR0_PG_MASK )) {
@@ -755,25 +759,25 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) }if (la57) { - pml5e_addr = ((env->cr[3 ] & ~0xfff ) + + pml5e_addr = ((env->cr[3 ] & ~0xfff & me_mask) + (((addr >>48 ) &0x1ff ) <<3 )) & a20_mask; - pml5e =ldq_phys_debug (cs, pml5e_addr); + pml5e =ldq_phys_debug (cs, pml5e_addr) & me_mask;if (!(pml5e &PG_PRESENT_MASK )) {return -1 ; } }else { - pml5e = env->cr[3 ]; + pml5e = env->cr[3 ] & me_mask; } pml4e_addr = ((pml5e &PG_ADDRESS_MASK ) + (((addr >>39 ) &0x1ff ) <<3 )) & a20_mask; - pml4e =ldq_phys_debug (cs, pml4e_addr); + pml4e =ldq_phys_debug (cs, pml4e_addr) & me_mask;if (!(pml4e &PG_PRESENT_MASK )) {return -1 ; } pdpe_addr = ((pml4e &PG_ADDRESS_MASK ) + (((addr >>30 ) &0x1ff ) <<3 )) & a20_mask; - pdpe =x86_ldq_phys (cs, pdpe_addr); + pdpe =ldq_phys_debug (cs, pdpe_addr) & me_mask;if (!(pdpe &PG_PRESENT_MASK )) {return -1 ; }
@@ -786,16 +790,16 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) } else#endif { - pdpe_addr = ((env->cr[3 ] & ~0x1f ) + ((addr >>27 ) &0x18 )) & - a20_mask; - pdpe =ldq_phys_debug (cs, pdpe_addr); + pdpe_addr = ((env->cr[3 ] & ~0x1f & me_mask) + ((addr >>27 ) &0x18 )) + & a20_mask; + pdpe =ldq_phys_debug (cs, pdpe_addr) & me_mask;if (!(pdpe &PG_PRESENT_MASK ))return -1 ; } pde_addr = ((pdpe &PG_ADDRESS_MASK ) + (((addr >>21 ) &0x1ff ) <<3 )) & a20_mask; - pde =ldq_phys_debug (cs, pde_addr); + pde =ldq_phys_debug (cs, pde_addr) & me_mask;if (!(pde &PG_PRESENT_MASK )) {return -1 ; }
@@ -808,7 +812,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) pte_addr = ((pde &PG_ADDRESS_MASK ) + (((addr >>12 ) &0x1ff ) <<3 )) & a20_mask; page_size =4096 ; - pte =ldq_phys_debug (cs, pte_addr); + pte =ldq_phys_debug (cs, pte_addr) & me_mask; }if (!(pte &PG_PRESENT_MASK )) {return -1 ;
@@ -817,8 +821,9 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)uint32_t pde;/* page directory entry */ - pde_addr = ((env->cr[3 ] & ~0xfff ) + ((addr >>20 ) &0xffc )) & a20_mask; - pde =ldl_phys_debug (cs, pde_addr); + pde_addr = ((env->cr[3 ] & ~0xfff & me_mask) + ((addr >>20 ) &0xffc )) + & a20_mask; + pde =ldl_phys_debug (cs, pde_addr) & me_mask;if (!(pde &PG_PRESENT_MASK ))return -1 ;if ((pde &PG_PSE_MASK ) && (env->cr[4 ] &CR4_PSE_MASK )) {
@@ -827,7 +832,7 @@ hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) }else {/* page directory entry */ pte_addr = ((pde & ~0xfff ) + ((addr >>10 ) &0xffc )) & a20_mask; - pte =ldl_phys_debug (cs, pte_addr); + pte =ldl_phys_debug (cs, pte_addr) & me_mask;if (!(pte &PG_PRESENT_MASK )) {return -1 ; }
target/i386/monitor.c +45 -24
216 lines not on this page · raw message
Signature · 1 line
2.14.3
PATCH v7 23/26 include: add psp-sev.h header file
In reply to Brijesh SinghThe header file provide the ioctl command and structure to communicate with /dev/sev device.
--- linux-headers/linux/psp-sev.h | 142 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 linux-headers/linux/psp-sev.h
linux-headers/linux/psp-sev.h new file +142 -0
@@ -0,0 +1,142 @@ +/* +* Userspace interface for AMD Secure Encrypted Virtualization (SEV) +* platform management commands. +* +* Copyright (C) 2016-2017 Advanced Micro Devices, Inc. +* +* Author: Brijesh Singh <[email protected]> +* +* SEV spec 0.14 is available at: +* http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +*/ + +#ifndef __PSP_SEV_USER_H__ +#define __PSP_SEV_USER_H__ + +#include <linux/types.h> + +/** +* SEV platform commands +*/ +enum { +SEV_FACTORY_RESET =0 , +SEV_PLATFORM_STATUS , +SEV_PEK_GEN , +SEV_PEK_CSR , +SEV_PDH_GEN , +SEV_PDH_CERT_EXPORT , +SEV_PEK_CERT_IMPORT , + +SEV_MAX , +}; + +/** +* SEV Firmware status code +*/ +typedef enum { +SEV_RET_SUCCESS =0 , +SEV_RET_INVALID_PLATFORM_STATE , +SEV_RET_INVALID_GUEST_STATE , +SEV_RET_INAVLID_CONFIG , +SEV_RET_INVALID_LEN , +SEV_RET_ALREADY_OWNED , +SEV_RET_INVALID_CERTIFICATE , +SEV_RET_POLICY_FAILURE , +SEV_RET_INACTIVE , +SEV_RET_INVALID_ADDRESS , +SEV_RET_BAD_SIGNATURE , +SEV_RET_BAD_MEASUREMENT , +SEV_RET_ASID_OWNED , +SEV_RET_INVALID_ASID , +SEV_RET_WBINVD_REQUIRED , +SEV_RET_DFFLUSH_REQUIRED , +SEV_RET_INVALID_GUEST , +SEV_RET_INVALID_COMMAND , +SEV_RET_ACTIVE , +SEV_RET_HWSEV_RET_PLATFORM , +SEV_RET_HWSEV_RET_UNSAFE , +SEV_RET_UNSUPPORTED , +SEV_RET_MAX , +}sev_ret_code ; + +/** +* struct sev_user_data_status - PLATFORM_STATUS command parameters +* +* @major: major API version +* @minor: minor API version +* @state: platform state +* @flags: platform config flags +* @build: firmware build id for API version +* @guest_count: number of active guests +*/ +struct sev_user_data_status { +__u8 api_major;/* Out */ +__u8 api_minor;/* Out */ +__u8 state;/* Out */ +__u32 flags;/* Out */ +__u8 build;/* Out */ +__u32 guest_count;/* Out */ +} __attribute__((packed)); + +/** +* struct sev_user_data_pek_csr - PEK_CSR command parameters +* +* @address: PEK certificate chain +* @length: length of certificate +*/ +struct sev_user_data_pek_csr { +__u64 address;/* In */ +__u32 length;/* In/Out */ +} __attribute__((packed)); + +/** +* struct sev_user_data_cert_import - PEK_CERT_IMPORT command parameters +* +* @pek_address: PEK certificate chain +* @pek_len: length of PEK certificate +* @oca_address: OCA certificate chain +* @oca_len: length of OCA certificate +*/ +struct sev_user_data_pek_cert_import { +__u64 pek_cert_address;/* In */ +__u32 pek_cert_len;/* In */ +__u64 oca_cert_address;/* In */ +__u32 oca_cert_len;/* In */ +} __attribute__((packed)); + +/** +* struct sev_user_data_pdh_cert_export - PDH_CERT_EXPORT command parameters +* +* @pdh_address: PDH certificate address +* @pdh_len: length of PDH certificate +* @cert_chain_address: PDH certificate chain +* @cert_chain_len: length of PDH certificate chain +*/ +struct sev_user_data_pdh_cert_export { +__u64 pdh_cert_address;/* In */ +__u32 pdh_cert_len;/* In/Out */ +__u64 cert_chain_address;/* In */ +__u32 cert_chain_len;/* In/Out */ +} __attribute__((packed)); + +/** +* struct sev_issue_cmd - SEV ioctl parameters +* +* @cmd: SEV commands to execute +* @opaque: pointer to the command structure +* @error: SEV FW return code on failure +*/ +struct sev_issue_cmd { +__u32 cmd;/* In */ +__u64 data;/* In */ +__u32 error;/* Out */ +} __attribute__((packed)); + +#define SEV_IOC_TYPE 'S' +#define SEV_ISSUE_CMD _IOWR(SEV_IOC_TYPE, 0x0, struct sev_issue_cmd) + +#endif /* __PSP_USER_SEV_H */
Signature · 1 line
2.14.3
PATCH v7 24/26 sev: add support to query PLATFORM_STATUS command
In reply to Brijesh SinghThe command is used to query the SEV API version and build id.
--- accel/kvm/sev.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
accel/kvm/sev.c +33 -0
@@ -21,6 +21,9 @@#include "trace.h" #include "qapi-event.h" +#include <sys/ioctl.h> +#include <linux/psp-sev.h> +#define DEFAULT_GUEST_POLICY 0x1/* disable debug */ #define DEFAULT_SEV_DEVICE "/dev/sev"#define GUEST_POLICY_DBG_BIT 0x1
@@ -91,6 +94,22 @@ sev_ioctl(int cmd, void *data, int *error)return r; } +static int +sev_platform_ioctl (int cmd,void *data,int *error) +{ +int r; +struct sev_issue_cmd arg; + + arg.cmd = cmd; + arg.data = (unsigned long )data; + r =ioctl (sev_fd,SEV_ISSUE_CMD , &arg); +if (error) { + *error = arg.error; + } + +return r; +} +static const char *fw_error_to_str (int code) {
@@ -380,6 +399,20 @@ sev_enabled(void)void sev_get_fw_version (uint8_t *major,uint8_t *minor,uint8_t *build) { +struct sev_user_data_status status = {}; +int r, err; + + r =sev_platform_ioctl (SEV_PLATFORM_STATUS , &status, &err); +if (r) { +error_report ("%s: failed to get platform status ret=%d" +"fw_error='%d: %s'" , __func__, r, err, +fw_error_to_str (err)); +return ; + } + + *major = status.api_major; + *minor = status.api_minor; + *build = status.build; }void
Signature · 1 line
2.14.3
PATCH v7 25/26 sev: add support to KVM_SEV_GUEST_STATUS
In reply to Brijesh SinghThe command is used to query the current SEV guest status. We use this command to query the guest policy for QMP query-sev command.
--- accel/kvm/sev.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
accel/kvm/sev.c +12 -0
@@ -418,6 +418,18 @@ sev_get_fw_version(uint8_t *major, uint8_t *minor, uint8_t *build)void sev_get_policy (uint32_t *policy) { +struct kvm_sev_guest_status status = {}; +int r, err; + + r =sev_ioctl (KVM_SEV_GUEST_STATUS , &status, &err); +if (r) { +error_report ("%s: failed to get platform status ret=%d " +"fw_error='%d: %s'" , __func__, r, err, +fw_error_to_str (err)); +return ; + } + + *policy = status.policy; } static int
Signature · 1 line
2.14.3
PATCH v7 26/26 qmp: add query-sev-launch-measure command
In reply to Brijesh SinghThe command can be used by libvirt to retrieve the measurement of SEV guest. This measurement is a signature of the memory contents that was encrypted through the LAUNCH_UPDATE_DATA.
--- qapi-schema.json | 30 ++++++++++++++++++++++++++++++ qmp.c | 14 ++++++++++++++ 2 files changed, 44 insertions(+)
qapi-schema.json +30 -0
@@ -3236,3 +3236,33 @@ # ## { 'command': 'query-sev', 'returns': 'SevInfo' } + +## +# @SevLaunchMeasureInfo: +# +# SEV Guest Launch measurement information +# +# @data: the measurement value encoded in base64 +# +# Since: 2.12 +# +# Notes: If measurement is not available then a null measurement is returned. +## +{ 'struct': 'SevLaunchMeasureInfo', 'data': {'data': 'str'} } + +## +# @query-sev-launch-measure: +# +# Query the SEV guest launch information. +# +# Returns: The @SevLaunchMeasureInfo for the guest +# +# Since: 2.12 +# +# Example: +# +# -> { "execute": "query-sev-launch-measure" } +# <- { "return": { "data": "4l8LXeNlSPUDlXPJG5966/8%YZ" } } +# +## +{ 'command': 'query-sev-launch-measure', 'returns': 'SevLaunchMeasureInfo' }
qmp.c +14 -0
@@ -733,3 +733,17 @@ SevInfo *qmp_query_sev(Error **errp)return info; } + +SevLaunchMeasureInfo *qmp_query_sev_launch_measure (Error **errp) +{ +SevLaunchMeasureInfo *info =NULL ; + +if (sev_enabled ()) { + info =g_malloc0 (sizeof (*info)); + info->data =sev_get_launch_measurement (); + }else { +error_setg (errp,"SEV is not enabled" ); + } + +return info; +}
Signature · 1 line
2.14.3
Re: [PATCH v7 26/26] qmp: add query-sev-launch-measure command
Quoted 22 lines · Brijesh Singh
The command can be used by libvirt to retrieve the measurement of SEV guest. This measurement is a signature of the memory contents that was encrypted through the
LAUNCH_UPDATE_DATA.Cc "Daniel P. Berrangé" <[email protected]>Cc "Dr. David Alan Gilbert" <[email protected]>Cc Markus Armbruster <[email protected]>Signed-off-by Brijesh Singh <[email protected]>--- qapi-schema.json | 30 ++++++++++++++++++++++++++++++ qmp.c | 14 ++++++++++++++ 2 files changed, 44 insertions(+)qapi-schema.json +1
-0@@ -3236,3 +3236,33 @@ # ## { 'command': 'query-sev', 'returns': 'SevInfo' } +
qapi-schema.json line 3249
Quoted 10 lines
qapi-schema.json +10
-0+## +# @SevLaunchMeasureInfo: +# +# SEV Guest Launch measurement information +# +# @data: the measurement value encoded in base64 +# +# Since: 2.12 +# +# Notes: If measurement is not available then a null measurement is returned.
Null measurement, as in empty string? Would it be better to have query-sev-launch-measure return an error instead of an SevLaunchMeasureInfo with a null measurement in that case?
Quoted 17 lines
qapi-schema.json +17
-0+## +{ 'struct': 'SevLaunchMeasureInfo', 'data': {'data': 'str'} } + +## +# @query-sev-launch-measure: +# +# Query the SEV guest launch information. +# +# Returns: The @SevLaunchMeasureInfo for the guest +# +# Since: 2.12 +# +# Example: +# +# -> { "execute": "query-sev-launch-measure" } +# <- { "return": { "data": "4l8LXeNlSPUDlXPJG5966/8%YZ" } } +#
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [PATCH v7 26/26] qmp: add query-sev-launch-measure command
On 02/07/2018 10:29 AM, Eric Blake wrote:
...
Quoted 8 lines
Quoted 5 lines
qapi-schema.json +4
-0+# +# Since: 2.12 +# +# Notes: If measurement is not available then a null measurement isreturned.
Null measurement, as in empty string? Would it be better to have query-sev-launch-measure return an error instead of an SevLaunchMeasureInfo with a null measurement in that case?
Yes, an empty string when measurement is not available. I can certainly change it to return an error when measurement is not available.
Quoted 19 lines
Quoted 17 lines
qapi-schema.json +17
-0+## +{ 'struct': 'SevLaunchMeasureInfo', 'data': {'data': 'str'} } + +## +# @query-sev-launch-measure: +# +# Query the SEV guest launch information. +# +# Returns: The @SevLaunchMeasureInfo for the guest +# +# Since: 2.12 +# +# Example: +# +# -> { "execute": "query-sev-launch-measure" } +# <- { "return": { "data": "4l8LXeNlSPUDlXPJG5966/8%YZ" } } +#