From patchwork Tue May 19 05:20:15 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Kerr X-Patchwork-Id: 473728 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id AD6571400A0 for ; Tue, 19 May 2015 15:20:39 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 809B61A0017 for ; Tue, 19 May 2015 15:20:39 +1000 (AEST) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from ozlabs.org (ozlabs.org [103.22.144.67]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 18FA01A0273 for ; Tue, 19 May 2015 15:20:20 +1000 (AEST) Received: by ozlabs.org (Postfix, from userid 1023) id F1C66140DB6; Tue, 19 May 2015 15:20:19 +1000 (AEST) MIME-Version: 1.0 Message-Id: <1432012815.75450.914693199325.4.gpush@pablo> In-Reply-To: <1432012815.74155.190719829775.0.gpush@pablo> To: skiboot@lists.ozlabs.org From: Jeremy Kerr Date: Tue, 19 May 2015 13:20:15 +0800 Subject: [Skiboot] [PATCH 4/7] core: Add mem_region_next X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" This change adds a function to iterate mem_regions. Signed-off-by: Jeremy Kerr --- core/mem_region.c | 14 +++ core/test/Makefile.check | 1 core/test/run-mem_region_next.c | 117 ++++++++++++++++++++++++++++++++ include/skiboot.h | 3 4 files changed, 135 insertions(+) diff --git a/core/mem_region.c b/core/mem_region.c index 40f3293..5e9393d 100644 --- a/core/mem_region.c +++ b/core/mem_region.c @@ -1052,3 +1052,17 @@ void mem_region_add_dt_reserved(void) free(names); free(ranges); } + +struct mem_region *mem_region_next(struct mem_region *region) +{ + struct list_node *node; + + assert(lock_held_by_me(&mem_region_lock)); + + node = region ? ®ion->list : ®ions.n; + + if (node->next == ®ions.n) + return NULL; + + return list_entry(node->next, struct mem_region, list); +} diff --git a/core/test/Makefile.check b/core/test/Makefile.check index c6cf835..939add6 100644 --- a/core/test/Makefile.check +++ b/core/test/Makefile.check @@ -4,6 +4,7 @@ CORE_TEST := core/test/run-device \ core/test/run-malloc \ core/test/run-malloc-speed \ core/test/run-mem_region_init \ + core/test/run-mem_region_next \ core/test/run-mem_region_release_unused \ core/test/run-mem_region_release_unused_noalloc \ core/test/run-mem_region_reservations \ diff --git a/core/test/run-mem_region_next.c b/core/test/run-mem_region_next.c new file mode 100644 index 0000000..209ce67 --- /dev/null +++ b/core/test/run-mem_region_next.c @@ -0,0 +1,117 @@ +/* Copyright 2013-2015 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + * implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#define BITS_PER_LONG (sizeof(long) * 8) +/* Don't include this, it's PPC-specific */ +#define __CPU_H +static unsigned int cpu_max_pir = 1; +struct cpu_thread { + unsigned int chip_id; +}; + +#include +#include + +/* Use these before we override definitions below. */ +static void *real_malloc(size_t size) +{ + return malloc(size); +} + +static void real_free(void *p) +{ + return free(p); +} + +#undef malloc +#undef free + +#include + +#define is_rodata(p) true + +#include "../mem_region.c" +#include "../malloc.c" +#include "../device.c" + +#include +#include + +void lock(struct lock *l) +{ + assert(!l->lock_val); + l->lock_val++; +} + +void unlock(struct lock *l) +{ + assert(l->lock_val); + l->lock_val--; +} + +bool lock_held_by_me(struct lock *l) +{ + return l->lock_val; +} + + +#define TEST_HEAP_ORDER 12 +#define TEST_HEAP_SIZE (1ULL << TEST_HEAP_ORDER) + +int main(void) +{ + struct mem_region *r; + char *test_heap; + + /* Use malloc for the heap, so valgrind can find issues. */ + test_heap = real_malloc(TEST_HEAP_SIZE); + skiboot_heap.start = (unsigned long)test_heap; + skiboot_heap.len = TEST_HEAP_SIZE; + + lock(&mem_region_lock); + + /* empty regions */ + r = mem_region_next(NULL); + assert(!r); + + r = new_region("test.1", 0x1000, 0x1000, NULL, REGION_RESERVED); + assert(add_region(r)); + r = new_region("test.2", 0x2000, 0x1000, NULL, REGION_RESERVED); + assert(add_region(r)); + mem_regions_finalised = true; + + r = mem_region_next(NULL); + assert(r); + assert(r->start = 0x1000); + assert(r->len = 0x1000); + assert(r->type = REGION_RESERVED); + + r = mem_region_next(r); + assert(r); + assert(r->start = 0x2000); + assert(r->len = 0x1000); + assert(r->type = REGION_RESERVED); + + r = mem_region_next(r); + assert(!r); + + unlock(&mem_region_lock); + real_free(test_heap); + + return 0; +} diff --git a/include/skiboot.h b/include/skiboot.h index 9751a31..e92d38e 100644 --- a/include/skiboot.h +++ b/include/skiboot.h @@ -46,6 +46,9 @@ /* Readonly section start and end. */ extern char __rodata_start[], __rodata_end[]; +struct mem_region; +extern struct mem_region *mem_region_next(struct mem_region *region); + static inline bool is_rodata(const void *p) { return ((const char *)p >= __rodata_start && (const char *)p < __rodata_end);