From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59381) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W448s-0008ER-UA for qemu-devel@nongnu.org; Fri, 17 Jan 2014 02:47:08 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W448m-0003Tc-Bi for qemu-devel@nongnu.org; Fri, 17 Jan 2014 02:47:02 -0500 Received: from fgwmail6.fujitsu.co.jp ([192.51.44.36]:38934) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W448l-0003Sn-NT for qemu-devel@nongnu.org; Fri, 17 Jan 2014 02:46:56 -0500 Received: from m1.gw.fujitsu.co.jp (unknown [10.0.50.71]) by fgwmail6.fujitsu.co.jp (Postfix) with ESMTP id 7C8FA3EE0D1 for ; Fri, 17 Jan 2014 16:46:54 +0900 (JST) Received: from smail (m1 [127.0.0.1]) by outgoing.m1.gw.fujitsu.co.jp (Postfix) with ESMTP id 671B145DE5C for ; Fri, 17 Jan 2014 16:46:54 +0900 (JST) Received: from s1.gw.fujitsu.co.jp (s1.gw.nic.fujitsu.com [10.0.50.91]) by m1.gw.fujitsu.co.jp (Postfix) with ESMTP id 4C9B645DE5A for ; Fri, 17 Jan 2014 16:46:54 +0900 (JST) Received: from s1.gw.fujitsu.co.jp (localhost.localdomain [127.0.0.1]) by s1.gw.fujitsu.co.jp (Postfix) with ESMTP id 3D1ED1DB804E for ; Fri, 17 Jan 2014 16:46:54 +0900 (JST) Received: from s00.gw.fujitsu.co.jp (s00.gw.nic.fujitsu.com [133.161.11.15]) by s1.gw.fujitsu.co.jp (Postfix) with ESMTP id E217AE08003 for ; Fri, 17 Jan 2014 16:46:53 +0900 (JST) Received: from s00.gw.fujitsu.co.jp (kw-mxio2.gw.nic.fujitsu.com [10.0.237.142]) by s00.gw.fujitsu.co.jp (Postfix) with ESMTP id 01FEC1180BC for ; Fri, 17 Jan 2014 16:46:52 +0900 (JST) Received: from G08FNSTD100518.localdomain (unknown [10.167.226.68]) by s00.gw.fujitsu.co.jp (Postfix) with ESMTP id A67BF8A00C for ; Fri, 17 Jan 2014 16:46:51 +0900 (JST) From: qiaonuohan Date: Fri, 17 Jan 2014 15:46:15 +0800 Message-Id: <1389944779-31899-10-git-send-email-qiaonuohan@cn.fujitsu.com> In-Reply-To: <1389944779-31899-1-git-send-email-qiaonuohan@cn.fujitsu.com> References: <1389944779-31899-1-git-send-email-qiaonuohan@cn.fujitsu.com> Subject: [Qemu-devel] [PATCH 09/13 v7] dump: add API to write dump_bitmap List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: lersek@redhat.com, stefanha@gmail.com, lcapitulino@redhat.com, afaerber@suse.de, eblake@redhat.com Cc: kumagai-atsushi@mxc.nes.nec.co.jp, qiaonuohan , anderson@redhat.com, qemu-devel@nongnu.org functions are used to write 1st and 2nd dump_bitmap of kdump-compressed format, which is used to indicate whether the corresponded page is existed in vmcore. 1st and 2nd dump_bitmap are same, because dump level is specified to 1 here. Signed-off-by: Qiao Nuohan Reviewed-by: Laszlo Ersek --- dump.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ include/sysemu/dump.h | 2 + 2 files changed, 174 insertions(+), 0 deletions(-) diff --git a/dump.c b/dump.c index a7fdc3f..26a1756 100644 --- a/dump.c +++ b/dump.c @@ -1001,6 +1001,178 @@ static int write_dump_header(DumpState *s) } } +/* + * set dump_bitmap sequencely. the bit before last_pfn is not allowed to be + * rewritten, so if need to set the first bit, set last_pfn and pfn to 0. + * set_dump_bitmap will always leave the recently set bit un-sync. And setting + * (last bit + sizeof(buf) * 8) to 0 will do flushing the content in buf into + * vmcore, ie. synchronizing un-sync bit into vmcore. + */ +static int set_dump_bitmap(uint64_t last_pfn, uint64_t pfn, bool value, + uint8_t *buf, DumpState *s) +{ + off_t old_offset, new_offset; + off_t offset_bitmap1, offset_bitmap2; + uint32_t byte, bit; + + /* should not set the previous place */ + assert(last_pfn <= pfn); + + /* + * if the bit needed to be set is not cached in buf, flush the data in buf + * to vmcore firstly. + * making new_offset be bigger than old_offset can also sync remained data + * into vmcore. + */ + old_offset = BUFSIZE_BITMAP * (last_pfn / PFN_BUFBITMAP); + new_offset = BUFSIZE_BITMAP * (pfn / PFN_BUFBITMAP); + + while (old_offset < new_offset) { + /* calculate the offset and write dump_bitmap */ + offset_bitmap1 = s->offset_dump_bitmap + old_offset; + if (write_buffer(s->fd, offset_bitmap1, buf, + BUFSIZE_BITMAP) < 0) { + return -1; + } + + /* dump level 1 is chosen, so 1st and 2nd bitmap are same */ + offset_bitmap2 = s->offset_dump_bitmap + s->len_dump_bitmap + + old_offset; + if (write_buffer(s->fd, offset_bitmap2, buf, + BUFSIZE_BITMAP) < 0) { + return -1; + } + + memset(buf, 0, BUFSIZE_BITMAP); + old_offset += BUFSIZE_BITMAP; + } + + /* get the exact place of the bit in the buf, and set it */ + byte = (pfn % PFN_BUFBITMAP) / CHAR_BIT; + bit = (pfn % PFN_BUFBITMAP) % CHAR_BIT; + if (value) { + buf[byte] |= 1u << bit; + } else { + buf[byte] &= ~(1u << bit); + } + + return 0; +} + +/* + * exam every page and return the page from number and the address of the page. + * pfnptr and bufptr can be NULL. note: the blocks here is supposed to reflect + * guest-phys blocks, so block->target_start and block->target_end should be + * interal multiples of the target page size. + */ +static int get_next_page(uint64_t *pfnptr, uint8_t **bufptr, DumpState *s) +{ + static GuestPhysBlock *block; + static uint64_t pfn; + hwaddr addr; + uint8_t *buf; + + /* block == NULL means the start of the iteration */ + if (!block) { + block = QTAILQ_FIRST(&s->guest_phys_blocks.head); + assert(block->target_start % s->page_size == 0); + assert(block->target_end % s->page_size == 0); + pfn = paddr_to_pfn(block->target_start, s->page_shift); + if (pfnptr) { + *pfnptr = pfn; + } + if (bufptr) { + *bufptr = block->host_addr; + } + return 1; + } + + pfn++; + addr = pfn_to_paddr(pfn, s->page_shift); + + if ((addr >= block->target_start) && + (addr + s->page_size <= block->target_end)) { + buf = block->host_addr + (addr - block->target_start); + } else { + /* the next page is in the next block */ + block = QTAILQ_NEXT(block, next); + /* + * iteration comes to end and block is set to NULL, next time when + * get_next_page is called, the iteration will start from the first + * block + */ + if (!block) { + return 0; + } + assert(block->target_start % s->page_size == 0); + assert(block->target_end % s->page_size == 0); + pfn = paddr_to_pfn(block->target_start, s->page_shift); + buf = block->host_addr; + } + + if (pfnptr) { + *pfnptr = pfn; + } + if (bufptr) { + *bufptr = buf; + } + + return 1; +} + +static int write_dump_bitmap(DumpState *s) +{ + int ret = 0; + uint64_t last_pfn, pfn; + void *dump_bitmap_buf; + size_t num_dumpable; + + /* dump_bitmap_buf is used to store dump_bitmap temporarily */ + dump_bitmap_buf = g_malloc0(BUFSIZE_BITMAP); + + num_dumpable = 0; + last_pfn = 0; + + /* + * exam memory page by page, and set the bit in dump_bitmap corresponded + * to the existing page. + */ + while (get_next_page(&pfn, NULL, s)) { + ret = set_dump_bitmap(last_pfn, pfn, true, dump_bitmap_buf, s); + if (ret < 0) { + dump_error(s, "dump: failed to set dump_bitmap.\n"); + ret = -1; + goto out; + } + + last_pfn = pfn; + num_dumpable++; + } + + /* + * set_dump_bitmap will always leave the recently set bit un-sync. Here we + * set last_pfn + PFN_BUFBITMAP to 0 and those set but un-sync bit will be + * synchronized into vmcore. + */ + if (num_dumpable > 0) { + ret = set_dump_bitmap(last_pfn, last_pfn + PFN_BUFBITMAP, false, + dump_bitmap_buf, s); + if (ret < 0) { + dump_error(s, "dump: failed to sync dump_bitmap.\n"); + ret = -1; + goto out; + } + } + + /* number of dumpable pages that will be dumped later */ + s->num_dumpable = num_dumpable; + +out: + g_free(dump_bitmap_buf); + + return ret; +} + static ram_addr_t get_start_block(DumpState *s) { GuestPhysBlock *block; diff --git a/include/sysemu/dump.h b/include/sysemu/dump.h index dfee238..6d4d0bc 100644 --- a/include/sysemu/dump.h +++ b/include/sysemu/dump.h @@ -39,6 +39,8 @@ #define PHYS_BASE (0) #define DUMP_LEVEL (1) #define DISKDUMP_HEADER_BLOCKS (1) +#define BUFSIZE_BITMAP (TARGET_PAGE_SIZE) +#define PFN_BUFBITMAP (CHAR_BIT * BUFSIZE_BITMAP) typedef struct ArchDumpInfo { int d_machine; /* Architecture */ -- 1.7.1