From patchwork Tue Aug 23 15:16:07 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 9295815 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 8CEFF607F0 for ; Tue, 23 Aug 2016 15:19:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7E1F228CB8 for ; Tue, 23 Aug 2016 15:19:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 72ACA28CBD; Tue, 23 Aug 2016 15:19:00 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id BABF028CB8 for ; Tue, 23 Aug 2016 15:18:59 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bcDRV-0002pi-TW; Tue, 23 Aug 2016 15:16:45 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bcDRU-0002mP-2x; Tue, 23 Aug 2016 15:16:44 +0000 Received: from [85.158.143.35] by server-9.bemta-6.messagelabs.com id 35/92-28857-BD86CB75; Tue, 23 Aug 2016 15:16:43 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrJLMWRWlGSWpSXmKPExsVyuP0Ov+6tjD3 hBrvO8Fm8/bqE2eL7lslMDkwehz9cYQlgjGLNzEvKr0hgzVg8dStTwefAig3fuhkbGBfadDFy ckgIGEm8nfiPqYuRi0NIYCGjxPp7i9hBEmwCqhIbrp9iBbFFBNwkvnyfxQZiMwtESMz5cIoFx BYWMJf4dX4HE4jNAlR/qX8aWA2vgL3E9N3H2SEWyElcnzkdrIYTKH7hzF7mLkYOoGV2EnP3x0 1g5F7AyLCKUb04tagstUjXTC+pKDM9oyQ3MTNH19DATC83tbg4MT01JzGpWC85P3cTI9DLDEC wg3HeCf9DjJIcTEqivLpGe8KF+JLyUyozEosz4otKc1KLDzHKcHAoSfA+TgfKCRalpqdWpGXm AMMNJi3BwaMkwssJDDkh3uKCxNzizHSI1ClGRSlx3haQPgGQREZpHlwbLMQvMcpKCfMyAh0ix FOQWpSbWYIq/4pRnINRSZj3HsgUnsy8Erjpr4AWMwEtbrm/G2RxSSJCSqqBMTHJYI+Xge7HdZ M7JApWH/iyi6dfxeayxNfXyya6yvTe73hllxjMsSs/cGG1eByDkVR8xmu73V63bxf/P9J2ZbP qwwN747/+zpnxQ4hbfu6eF8edeFm+6DNOKy+X8XPjS+W+F/Lt0MJPEz/fX18xa8HKH2zy0xTb my+ujO0smnlo2hqla/MsTZVYijMSDbWYi4oTAZ2YP5dsAgAA X-Env-Sender: jgross@suse.com X-Msg-Ref: server-11.tower-21.messagelabs.com!1471965401!29723852!1 X-Originating-IP: [195.135.220.15] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.84; banners=-,-,- X-VirusChecked: Checked Received: (qmail 18589 invoked from network); 23 Aug 2016 15:16:42 -0000 Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by server-11.tower-21.messagelabs.com with DHE-RSA-CAMELLIA256-SHA encrypted SMTP; 23 Aug 2016 15:16:42 -0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id BA918ADB4; Tue, 23 Aug 2016 15:16:20 +0000 (UTC) From: Juergen Gross To: minios-devel@lists.xenproject.org, xen-devel@lists.xenproject.org Date: Tue, 23 Aug 2016 17:16:07 +0200 Message-Id: <1471965368-6159-22-git-send-email-jgross@suse.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1471965368-6159-1-git-send-email-jgross@suse.com> References: <1471965368-6159-1-git-send-email-jgross@suse.com> Cc: Juergen Gross , samuel.thibault@ens-lyon.org, wei.liu2@citrix.com Subject: [Xen-devel] [PATCH 21/22] mini-os: get physical memory map X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP On HVMlite we have to look at the physical memory map to know which memory frames are usable. In order to make life easier we define a dummy memory map for other domain types (pv and arm) which has just one entry with a maximum memory size. Signed-off-by: Juergen Gross Reviewed-by: Samuel Thibault --- arch/arm/mm.c | 8 ++++++ arch/x86/mm.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ arch/x86/setup.c | 1 + include/e820.h | 48 +++++++++++++++++++++++++++++++ include/x86/arch_mm.h | 4 +++ mm.c | 79 ++++++++++++++++++++++++++++++++------------------- 6 files changed, 183 insertions(+), 29 deletions(-) create mode 100644 include/e820.h diff --git a/arch/arm/mm.c b/arch/arm/mm.c index dbde162..8c156c4 100644 --- a/arch/arm/mm.c +++ b/arch/arm/mm.c @@ -7,6 +7,14 @@ #include uint32_t physical_address_offset; +struct e820entry e820_map[1] = { + { + .addr = 0, + .size = ULONG_MAX - 1, + .type = E820_RAM + } +}; +unsigned e820_entries = 1; unsigned long allocate_ondemand(unsigned long n, unsigned long alignment) { diff --git a/arch/x86/mm.c b/arch/x86/mm.c index 762599d..8dd90b8 100644 --- a/arch/x86/mm.c +++ b/arch/x86/mm.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #ifdef MM_DEBUG @@ -63,6 +64,15 @@ extern char stack[]; extern void page_walk(unsigned long va); #ifdef CONFIG_PARAVIRT +struct e820entry e820_map[1] = { + { + .addr = 0, + .size = ULONG_MAX - 1, + .type = E820_RAM + } +}; +unsigned e820_entries = 1; + void arch_mm_preinit(void *p) { start_info_t *si = p; @@ -102,10 +112,25 @@ desc_ptr idt_ptr = .base = (unsigned long)&idt, }; +struct e820entry e820_map[E820_MAX]; +unsigned e820_entries; + +static char *e820_types[E820_TYPES] = { + [E820_RAM] = "RAM", + [E820_RESERVED] = "Reserved", + [E820_ACPI] = "ACPI", + [E820_NVS] = "NVS", + [E820_UNUSABLE] = "Unusable", + [E820_PMEM] = "PMEM" +}; + void arch_mm_preinit(void *p) { long ret; domid_t domid = DOMID_SELF; + struct xen_memory_map memmap; + int i; + unsigned long pfn, max = 0; pt_base = page_table_base; first_free_pfn = PFN_UP(to_phys(&_end)); @@ -116,6 +141,53 @@ void arch_mm_preinit(void *p) do_exit(); } last_free_pfn = ret; + + memmap.nr_entries = E820_MAX; + set_xen_guest_handle(memmap.buffer, e820_map); + ret = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); + if ( ret < 0 ) + { + xprintk("could not get memory map\n"); + do_exit(); + } + e820_entries = memmap.nr_entries; + + for ( i = 0; i < e820_entries; i++ ) + { + if ( e820_map[i].type != E820_RAM ) + continue; + pfn = (e820_map[i].addr + e820_map[i].size) >> PAGE_SHIFT; + if ( pfn > max ) + max = pfn; + } + + if ( max < last_free_pfn ) + last_free_pfn = max; +} + +void arch_print_memmap(void) +{ + int i; + unsigned long from, to; + char *type; + char buf[12]; + + printk("Memory map:\n"); + for ( i = 0; i < e820_entries; i++ ) + { + if ( e820_map[i].type >= E820_TYPES || !e820_types[e820_map[i].type] ) + { + snprintf(buf, sizeof(buf), "%8x", e820_map[i].type); + type = buf; + } + else + { + type = e820_types[e820_map[i].type]; + } + from = e820_map[i].addr; + to = from + e820_map[i].size - 1; + printk("%012lx-%012lx: %s\n", from, to, type); + } } #endif diff --git a/arch/x86/setup.c b/arch/x86/setup.c index 948b08a..829b03c 100644 --- a/arch/x86/setup.c +++ b/arch/x86/setup.c @@ -150,6 +150,7 @@ static void print_start_of_day(void *p) printk(" flags: 0x%x\n", (unsigned int)si->flags); printk(" cmd_line: %s\n", cmdline); printk(" stack: %p-%p\n", stack, stack + sizeof(stack)); + arch_print_memmap(); } #endif diff --git a/include/e820.h b/include/e820.h new file mode 100644 index 0000000..920551c --- /dev/null +++ b/include/e820.h @@ -0,0 +1,48 @@ +/* -*- Mode:C; c-basic-offset:4; tab-width:4 -*- + * + * (C) 2016 - Juergen Gross, SUSE Linux GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef __E820_HEADER +#define __E820_HEADER + +/* PC BIOS standard E820 types and structure. */ +#define E820_RAM 1 +#define E820_RESERVED 2 +#define E820_ACPI 3 +#define E820_NVS 4 +#define E820_UNUSABLE 5 +#define E820_PMEM 7 +#define E820_TYPES 8 + +struct __packed e820entry { + uint64_t addr; + uint64_t size; + uint32_t type; +}; + +/* Maximum number of entries. */ +#define E820_MAX 128 + +extern struct e820entry e820_map[]; +extern unsigned e820_entries; + +#endif /*__E820_HEADER*/ diff --git a/include/x86/arch_mm.h b/include/x86/arch_mm.h index 9372f1e..ab8a53e 100644 --- a/include/x86/arch_mm.h +++ b/include/x86/arch_mm.h @@ -279,6 +279,10 @@ pgentry_t *need_pgt(unsigned long addr); void arch_mm_preinit(void *p); unsigned long alloc_virt_kernel(unsigned n_pages); +#ifndef CONFIG_PARAVIRT +void arch_print_memmap(void); +#endif + #endif #endif /* _ARCH_MM_H_ */ diff --git a/mm.c b/mm.c index c76be7f..932ceeb 100644 --- a/mm.c +++ b/mm.c @@ -43,6 +43,7 @@ #include #include #include +#include /********************* * ALLOCATION BITMAP @@ -147,10 +148,14 @@ static chunk_head_t free_tail[FREELIST_SIZE]; */ static void init_page_allocator(unsigned long min, unsigned long max) { - int i; + int i, m; unsigned long range; + unsigned long r_min, r_max; chunk_head_t *ch; chunk_tail_t *ct; + + printk("MM: Initialise page allocator for %lx(%lx)-%lx(%lx)\n", + (u_long)to_virt(min), min, (u_long)to_virt(max), max); for ( i = 0; i < FREELIST_SIZE; i++ ) { free_head[i] = &free_tail[i]; @@ -166,38 +171,57 @@ static void init_page_allocator(unsigned long min, unsigned long max) mm_alloc_bitmap_size = round_pgup(mm_alloc_bitmap_size); mm_alloc_bitmap = (unsigned long *)to_virt(min); min += mm_alloc_bitmap_size; - range = max - min; /* All allocated by default. */ memset(mm_alloc_bitmap, ~0, mm_alloc_bitmap_size); - /* Free up the memory we've been given to play with. */ - map_free(PHYS_PFN(min), range>>PAGE_SHIFT); - /* The buddy lists are addressed in high memory. */ - min = (unsigned long) to_virt(min); - max = (unsigned long) to_virt(max); - - while ( range != 0 ) + for ( m = 0; m < e820_entries; m++ ) { - /* - * Next chunk is limited by alignment of min, but also - * must not be bigger than remaining range. - */ - for ( i = PAGE_SHIFT; (1UL<<(i+1)) <= range; i++ ) - if ( min & (1UL<= ULONG_MAX ) + BUG(); + r_min = e820_map[m].addr; + r_max = r_min + e820_map[m].size; + if ( r_max <= min || r_min >= max ) + continue; + if ( r_min < min ) + r_min = min; + if ( r_max > max ) + r_max = max; - ch = (chunk_head_t *)min; - min += (1UL<level = i; - ch->next = free_head[i]; - ch->pprev = &free_head[i]; - ch->next->pprev = &ch->next; - free_head[i] = ch; - ct->level = i; + printk(" Adding memory range %lx-%lx\n", r_min, r_max); + + /* The buddy lists are addressed in high memory. */ + r_min = (unsigned long)to_virt(r_min); + r_max = (unsigned long)to_virt(r_max); + range = r_max - r_min; + + /* Free up the memory we've been given to play with. */ + map_free(PHYS_PFN(r_min), range >> PAGE_SHIFT); + + while ( range != 0 ) + { + /* + * Next chunk is limited by alignment of min, but also + * must not be bigger than remaining range. + */ + for ( i = PAGE_SHIFT; (1UL << (i + 1)) <= range; i++ ) + if ( r_min & (1UL << i) ) break; + + ch = (chunk_head_t *)r_min; + r_min += 1UL << i; + range -= 1UL << i; + ct = (chunk_tail_t *)r_min - 1; + i -= PAGE_SHIFT; + ch->level = i; + ch->next = free_head[i]; + ch->pprev = &free_head[i]; + ch->next->pprev = &ch->next; + free_head[i] = ch; + ct->level = i; + } } mm_alloc_bitmap_remap(); @@ -377,9 +401,6 @@ void init_mm(void) /* * now we can initialise the page allocator */ - printk("MM: Initialise page allocator for %lx(%lx)-%lx(%lx)\n", - (u_long)to_virt(PFN_PHYS(start_pfn)), (u_long)PFN_PHYS(start_pfn), - (u_long)to_virt(PFN_PHYS(max_pfn)), (u_long)PFN_PHYS(max_pfn)); init_page_allocator(PFN_PHYS(start_pfn), PFN_PHYS(max_pfn)); printk("MM: done\n");