From 7e5c9b895e52c76376386e54a0008c3a9e4a1698 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 25 Nov 2014 14:58:13 +0100 Subject: rtems: Move rtems_cache_aligned_malloc() Make sure also the size is cache aligned since otherwise we may have some overlap with the next allocation block. A cache invalidate on this area would be fatal. --- c/src/lib/libcpu/Makefile.am | 1 - c/src/lib/libcpu/bfin/Makefile.am | 2 +- c/src/lib/libcpu/i386/Makefile.am | 2 +- c/src/lib/libcpu/lm32/Makefile.am | 1 - c/src/lib/libcpu/m68k/Makefile.am | 1 - c/src/lib/libcpu/mips/Makefile.am | 2 +- c/src/lib/libcpu/nios2/Makefile.am | 1 - c/src/lib/libcpu/powerpc/Makefile.am | 2 +- c/src/lib/libcpu/sh/Makefile.am | 3 +- c/src/lib/libcpu/shared/src/cache_aligned_malloc.c | 43 ---------------------- c/src/lib/libcpu/sparc/Makefile.am | 2 +- c/src/lib/libcpu/sparc64/Makefile.am | 2 +- cpukit/libcsupport/Makefile.am | 1 + cpukit/libcsupport/src/cachealignedalloc.c | 28 ++++++++++++++ testsuites/sptests/spcache01/init.c | 31 ++++++++++++++++ testsuites/sptests/spcache01/spcache01.doc | 1 + testsuites/sptests/spcache01/spcache01.scn | 1 + 17 files changed, 69 insertions(+), 55 deletions(-) delete mode 100644 c/src/lib/libcpu/shared/src/cache_aligned_malloc.c create mode 100644 cpukit/libcsupport/src/cachealignedalloc.c diff --git a/c/src/lib/libcpu/Makefile.am b/c/src/lib/libcpu/Makefile.am index 7d3a43e4f6..630f5256a0 100644 --- a/c/src/lib/libcpu/Makefile.am +++ b/c/src/lib/libcpu/Makefile.am @@ -2,7 +2,6 @@ include_libcpudir = $(includedir)/libcpu EXTRA_DIST = EXTRA_DIST += shared/include/cache.h -EXTRA_DIST += shared/src/cache_aligned_malloc.c EXTRA_DIST += shared/src/cache_manager.c SUBDIRS = @libcpu_cpu_subdir@ diff --git a/c/src/lib/libcpu/bfin/Makefile.am b/c/src/lib/libcpu/bfin/Makefile.am index 06e2315457..cc0b70bfbe 100644 --- a/c/src/lib/libcpu/bfin/Makefile.am +++ b/c/src/lib/libcpu/bfin/Makefile.am @@ -54,7 +54,7 @@ include_libcpu_HEADERS += include/gpioRegs.h include_libcpu_HEADERS += ../shared/include/cache.h noinst_PROGRAMS += cache.rel -cache_rel_SOURCES = cache/cache.c ../shared/src/cache_aligned_malloc.c \ +cache_rel_SOURCES = cache/cache.c \ ../shared/src/cache_manager.c cache/cache_.h cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/cache cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/i386/Makefile.am b/c/src/lib/libcpu/i386/Makefile.am index 0330ba31ef..0869435025 100644 --- a/c/src/lib/libcpu/i386/Makefile.am +++ b/c/src/lib/libcpu/i386/Makefile.am @@ -11,7 +11,7 @@ include_libcpu_HEADERS += ../shared/include/cache.h include_libcpu_HEADERS += byteorder.h noinst_PROGRAMS += cache.rel -cache_rel_SOURCES = cache.c cache_.h ../shared/src/cache_aligned_malloc.c \ +cache_rel_SOURCES = cache.c cache_.h \ ../shared/src/cache_manager.c ../shared/include/cache.h cache_rel_CPPFLAGS = $(AM_CPPFLAGS) cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/lm32/Makefile.am b/c/src/lib/libcpu/lm32/Makefile.am index f51d0ebe35..e571885e1b 100644 --- a/c/src/lib/libcpu/lm32/Makefile.am +++ b/c/src/lib/libcpu/lm32/Makefile.am @@ -13,7 +13,6 @@ include_libcpudir = $(includedir)/libcpu include_libcpu_HEADERS = ../shared/include/cache.h noinst_PROGRAMS += shared/cache.rel shared_cache_rel_SOURCES = ../shared/src/no_cache.c shared/cache/cache_.h \ - ../shared/src/cache_aligned_malloc.c \ ../shared/src/cache_manager.c shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/m68k/Makefile.am b/c/src/lib/libcpu/m68k/Makefile.am index 19f500f1e1..45d7634b0f 100644 --- a/c/src/lib/libcpu/m68k/Makefile.am +++ b/c/src/lib/libcpu/m68k/Makefile.am @@ -13,7 +13,6 @@ include_libcpu_HEADERS = ../shared/include/cache.h noinst_PROGRAMS += shared/cache.rel shared_cache_rel_SOURCES = shared/cache/cache.c shared_cache_rel_SOURCES += shared/cache/cache_.h -shared_cache_rel_SOURCES += ../shared/src/cache_aligned_malloc.c shared_cache_rel_SOURCES += ../shared/src/cache_manager.c shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/mips/Makefile.am b/c/src/lib/libcpu/mips/Makefile.am index be4d2c0df7..b261afe92f 100644 --- a/c/src/lib/libcpu/mips/Makefile.am +++ b/c/src/lib/libcpu/mips/Makefile.am @@ -12,7 +12,7 @@ include_libcpudir = $(includedir)/libcpu include_libcpu_HEADERS = ../shared/include/cache.h noinst_PROGRAMS += shared/cache.rel -shared_cache_rel_SOURCES = shared/cache/cache.c ../shared/src/cache_aligned_malloc.c \ +shared_cache_rel_SOURCES = shared/cache/cache.c \ ../shared/src/cache_manager.c shared/cache/cache_.h shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/nios2/Makefile.am b/c/src/lib/libcpu/nios2/Makefile.am index 233b5a15b3..e249998acf 100644 --- a/c/src/lib/libcpu/nios2/Makefile.am +++ b/c/src/lib/libcpu/nios2/Makefile.am @@ -13,7 +13,6 @@ include_libcpudir = $(includedir)/libcpu include_libcpu_HEADERS = ../shared/include/cache.h noinst_PROGRAMS += shared/cache.rel shared_cache_rel_SOURCES = ../shared/src/no_cache.c shared/cache/cache_.h \ - ../shared/src/cache_aligned_malloc.c \ ../shared/src/cache_manager.c shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/powerpc/Makefile.am b/c/src/lib/libcpu/powerpc/Makefile.am index 5b83288c6e..f7d7e64e5c 100644 --- a/c/src/lib/libcpu/powerpc/Makefile.am +++ b/c/src/lib/libcpu/powerpc/Makefile.am @@ -72,7 +72,7 @@ include_libcpu_HEADERS += shared/include/cpuIdent.h # shared/cache noinst_PROGRAMS += shared/cache.rel shared_cache_rel_SOURCES = shared/src/cache_.h \ - ../shared/src/cache_aligned_malloc.c ../shared/src/cache_manager.c \ + ../shared/src/cache_manager.c \ ../shared/include/cache.h shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/src shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/sh/Makefile.am b/c/src/lib/libcpu/sh/Makefile.am index 532597fdbf..5c0a6ed960 100644 --- a/c/src/lib/libcpu/sh/Makefile.am +++ b/c/src/lib/libcpu/sh/Makefile.am @@ -8,8 +8,7 @@ include_libcpudir = $(includedir)/libcpu include_libcpu_HEADERS = ../shared/include/cache.h noinst_PROGRAMS += cache.rel -cache_rel_SOURCES = ../shared/src/cache_aligned_malloc.c -cache_rel_SOURCES += ../shared/src/cache_manager.c +cache_rel_SOURCES = ../shared/src/cache_manager.c cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/cache cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/shared/src/cache_aligned_malloc.c b/c/src/lib/libcpu/shared/src/cache_aligned_malloc.c deleted file mode 100644 index 17fc46814b..0000000000 --- a/c/src/lib/libcpu/shared/src/cache_aligned_malloc.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * RTEMS Cache Aligned Malloc - * - * - * COPYRIGHT (c) 1989-1999. - * On-Line Applications Research Corporation (OAR). - * - * The license and distribution terms for this file may be - * found in the file LICENSE in this distribution or at - * http://www.rtems.org/license/LICENSE. - */ - -#include - -#include -#include -#include - -/* - * rtems_cache_aligned_malloc - * - * DESCRIPTION: - * - * This function is used to allocate storage that spans an - * integral number of cache blocks. - */ - -void *rtems_cache_aligned_malloc ( - size_t nbytes -) -{ - /* - * Arrange to have the user storage start on the first cache - * block beyond the header. - */ -#if defined(CPU_DATA_CACHE_ALIGNMENT) - return (void *) ((((unsigned long) - malloc( nbytes + CPU_DATA_CACHE_ALIGNMENT - 1 )) - + CPU_DATA_CACHE_ALIGNMENT - 1 ) &(~(CPU_DATA_CACHE_ALIGNMENT - 1)) ); -#else - return malloc( nbytes ); -#endif -} diff --git a/c/src/lib/libcpu/sparc/Makefile.am b/c/src/lib/libcpu/sparc/Makefile.am index 9f8bacbe8a..ceed3bcc65 100644 --- a/c/src/lib/libcpu/sparc/Makefile.am +++ b/c/src/lib/libcpu/sparc/Makefile.am @@ -11,7 +11,7 @@ include_libcpu_HEADERS += include/libcpu/access.h noinst_PROGRAMS += cache.rel cache_rel_SOURCES = cache/cache.c cache/cache_.h \ - ../shared/src/cache_aligned_malloc.c ../shared/src/cache_manager.c + ../shared/src/cache_manager.c cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/cache cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) diff --git a/c/src/lib/libcpu/sparc64/Makefile.am b/c/src/lib/libcpu/sparc64/Makefile.am index a95672e8d8..b0a1f84bcb 100644 --- a/c/src/lib/libcpu/sparc64/Makefile.am +++ b/c/src/lib/libcpu/sparc64/Makefile.am @@ -27,7 +27,7 @@ shared_sparc64_syscall_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) noinst_PROGRAMS += shared/cache.rel shared_cache_rel_SOURCES = shared/cache/cache.c shared/cache/cache_.h \ - ../shared/src/cache_aligned_malloc.c ../shared/src/cache_manager.c + ../shared/src/cache_manager.c shared_cache_rel_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/shared/cache shared_cache_rel_LDFLAGS = $(RTEMS_RELLDFLAGS) endif diff --git a/cpukit/libcsupport/Makefile.am b/cpukit/libcsupport/Makefile.am index bd3f90a648..95c85c4ede 100644 --- a/cpukit/libcsupport/Makefile.am +++ b/cpukit/libcsupport/Makefile.am @@ -108,6 +108,7 @@ MALLOC_C_FILES = src/malloc_initialize.c src/calloc.c src/malloc.c \ src/rtems_heap_null_extend.c \ src/rtems_heap_extend.c \ src/rtems_heap_greedy.c +MALLOC_C_FILES += src/cachealignedalloc.c PASSWORD_GROUP_C_FILES = src/pwdgrp.c PASSWORD_GROUP_C_FILES += src/getgrent.c diff --git a/cpukit/libcsupport/src/cachealignedalloc.c b/cpukit/libcsupport/src/cachealignedalloc.c new file mode 100644 index 0000000000..a704859dbe --- /dev/null +++ b/cpukit/libcsupport/src/cachealignedalloc.c @@ -0,0 +1,28 @@ +/* + * RTEMS Cache Aligned Malloc + * + * + * COPYRIGHT (c) 1989-1999. + * On-Line Applications Research Corporation (OAR). + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.org/license/LICENSE. + */ + +#include +#include + +void *rtems_cache_aligned_malloc( size_t nbytes ) +{ + size_t line_size = rtems_cache_get_data_line_size(); + + if ( line_size > 0 ) { + /* Assume that the cache line size is a power of two */ + size_t m = line_size - 1; + + nbytes = (nbytes + m) & ~m; + } + + return rtems_heap_allocate_aligned_with_boundary( nbytes, line_size, 0 ); +} diff --git a/testsuites/sptests/spcache01/init.c b/testsuites/sptests/spcache01/init.c index e303436224..95777e1a28 100644 --- a/testsuites/sptests/spcache01/init.c +++ b/testsuites/sptests/spcache01/init.c @@ -17,6 +17,7 @@ #endif #include +#include #include #include @@ -379,12 +380,42 @@ static void test_timing(void) rtems_interrupt_lock_destroy(&lock); } +static void test_cache_aligned_alloc(void) +{ + void *p0; + void *p1; + size_t cls; + + printf("test rtems_cache_aligned_malloc()\n"); + + p0 = rtems_cache_aligned_malloc(1); + p1 = rtems_cache_aligned_malloc(1); + + rtems_test_assert(p0 != NULL); + rtems_test_assert(p1 != NULL); + + cls = rtems_cache_get_data_line_size(); + if (cls > 0) { + size_t m = cls - 1; + uintptr_t a0 = (uintptr_t) p0; + uintptr_t a1 = (uintptr_t) p1; + + rtems_test_assert(a1 - a0 > cls); + rtems_test_assert((a0 & m) == 0); + rtems_test_assert((a1 & m) == 0); + } + + free(p0); + free(p1); +} + static void Init(rtems_task_argument arg) { TEST_BEGIN(); test_data_flush_and_invalidate(); test_timing(); + test_cache_aligned_alloc(); TEST_END(); diff --git a/testsuites/sptests/spcache01/spcache01.doc b/testsuites/sptests/spcache01/spcache01.doc index bbc8f253c0..04e32a1e5d 100644 --- a/testsuites/sptests/spcache01/spcache01.doc +++ b/testsuites/sptests/spcache01/spcache01.doc @@ -13,6 +13,7 @@ directives: - rtems_cache_invalidate_entire_instruction() - rtems_cache_invalidate_multiple_data_lines() - rtems_cache_invalidate_multiple_instruction_lines() + - rtems_cache_aligned_malloc() concepts: diff --git a/testsuites/sptests/spcache01/spcache01.scn b/testsuites/sptests/spcache01/spcache01.scn index 80139451ef..4e7d53a9f6 100644 --- a/testsuites/sptests/spcache01/spcache01.scn +++ b/testsuites/sptests/spcache01/spcache01.scn @@ -42,4 +42,5 @@ invalidate multiple instruction duration with normal cache 680 ns duration with warm cache 640 ns duration with invalidated cache 2600 ns +test rtems_cache_aligned_malloc() *** END OF TEST SPCACHE 1 *** -- cgit v1.2.3