From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933794AbbGGUbT (ORCPT ); Tue, 7 Jul 2015 16:31:19 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:16572 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754468AbbGGUUx (ORCPT ); Tue, 7 Jul 2015 16:20:53 -0400 From: Yinghai Lu To: Kees Cook , "H. Peter Anvin" , Baoquan He Cc: linux-kernel@vger.kernel.org Subject: [PATCH 14/42] x86, kaslr: Add two functions which will be used later Date: Tue, 7 Jul 2015 13:20:00 -0700 Message-Id: <1436300428-21163-15-git-send-email-yinghai@kernel.org> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1436300428-21163-1-git-send-email-yinghai@kernel.org> References: <1436300428-21163-1-git-send-email-yinghai@kernel.org> X-Source-IP: aserv0021.oracle.com [141.146.126.233] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Baoquan He Add two functions mem_min_overlap() and store_slot_info() which will be used later. Given a memory region mem_min_overlap will iterate all avoid region to find the first one which overlap with it. store_slot_info() calculates the slot info of passed in region and store it into slot_areas[]. Signed-off-by: Baoquan He --- arch/x86/boot/compressed/aslr.c | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/arch/x86/boot/compressed/aslr.c b/arch/x86/boot/compressed/aslr.c index e3995f1..81070e9 100644 --- a/arch/x86/boot/compressed/aslr.c +++ b/arch/x86/boot/compressed/aslr.c @@ -214,6 +214,40 @@ static bool mem_avoid_overlap(struct mem_vector *img) return false; } +static unsigned long +mem_min_overlap(struct mem_vector *img, struct mem_vector *out) +{ + int i; + struct setup_data *ptr; + unsigned long min = img->start + img->size; + + for (i = 0; i < MEM_AVOID_MAX; i++) { + if (mem_overlaps(img, &mem_avoid[i]) && + (mem_avoid[i].start < min)) { + *out = mem_avoid[i]; + min = mem_avoid[i].start; + } + } + + /* Check all entries in the setup_data linked list. */ + ptr = (struct setup_data *)(unsigned long)real_mode->hdr.setup_data; + while (ptr) { + struct mem_vector avoid; + + avoid.start = (unsigned long)ptr; + avoid.size = sizeof(*ptr) + ptr->len; + + if (mem_overlaps(img, &avoid) && (avoid.start < min)) { + *out = avoid; + min = avoid.start; + } + + ptr = (struct setup_data *)(unsigned long)ptr->next; + } + + return min; +} + static unsigned long slots[CONFIG_RANDOMIZE_BASE_MAX_OFFSET / CONFIG_PHYSICAL_ALIGN]; @@ -230,6 +264,23 @@ static unsigned long slot_max; static unsigned long slot_area_index; +static void store_slot_info(struct mem_vector *region, unsigned long image_size) +{ + struct slot_area slot_area; + + slot_area.addr = region->start; + if (image_size <= CONFIG_PHYSICAL_ALIGN) + slot_area.num = region->size / CONFIG_PHYSICAL_ALIGN; + else + slot_area.num = (region->size - image_size) / + CONFIG_PHYSICAL_ALIGN + 1; + + if (slot_area.num > 0) { + slot_areas[slot_area_index++] = slot_area; + slot_max += slot_area.num; + } +} + static void slots_append(unsigned long addr) { /* Overflowing the slots list should be impossible. */ -- 1.8.4.5