| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Sergey Bondari
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <test.h>
|
|---|
| 30 | #include <mm/page.h>
|
|---|
| 31 | #include <mm/frame.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <arch/mm/page.h>
|
|---|
| 34 | #include <typedefs.h>
|
|---|
| 35 | #include <atomic.h>
|
|---|
| 36 | #include <proc/thread.h>
|
|---|
| 37 | #include <memw.h>
|
|---|
| 38 | #include <arch.h>
|
|---|
| 39 |
|
|---|
| 40 | #define MAX_FRAMES 256
|
|---|
| 41 |
|
|---|
| 42 | #define THREAD_RUNS 1
|
|---|
| 43 | #define THREADS 8
|
|---|
| 44 |
|
|---|
| 45 | static atomic_size_t thread_fail;
|
|---|
| 46 |
|
|---|
| 47 | static void falloc(void *arg)
|
|---|
| 48 | {
|
|---|
| 49 | uint8_t val = THREAD->tid % THREADS;
|
|---|
| 50 |
|
|---|
| 51 | uintptr_t *frames = (uintptr_t *)
|
|---|
| 52 | malloc(MAX_FRAMES * sizeof(uintptr_t));
|
|---|
| 53 | if (frames == NULL) {
|
|---|
| 54 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 55 | "Unable to allocate frames\n", THREAD->tid, CPU->id);
|
|---|
| 56 | atomic_inc(&thread_fail);
|
|---|
| 57 | return;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | for (unsigned int run = 0; run < THREAD_RUNS; run++) {
|
|---|
| 61 | for (size_t count = 1; count <= MAX_FRAMES; count++) {
|
|---|
| 62 | size_t bytes = FRAMES2SIZE(count);
|
|---|
| 63 |
|
|---|
| 64 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 65 | "Allocating %zu frames blocks (%zu bytes) ... \n", THREAD->tid,
|
|---|
| 66 | CPU->id, count, bytes);
|
|---|
| 67 |
|
|---|
| 68 | unsigned int allocated = 0;
|
|---|
| 69 | for (unsigned int i = 0; i < (MAX_FRAMES / count); i++) {
|
|---|
| 70 | frames[allocated] = frame_alloc(count, FRAME_ATOMIC, 0);
|
|---|
| 71 | if (frames[allocated]) {
|
|---|
| 72 | memsetb((void *) PA2KA(frames[allocated]), bytes, val);
|
|---|
| 73 | allocated++;
|
|---|
| 74 | } else
|
|---|
| 75 | break;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 79 | "%u blocks allocated.\n", THREAD->tid, CPU->id,
|
|---|
| 80 | allocated);
|
|---|
| 81 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 82 | "Deallocating ... \n", THREAD->tid, CPU->id);
|
|---|
| 83 |
|
|---|
| 84 | for (unsigned int i = 0; i < allocated; i++) {
|
|---|
| 85 | for (size_t k = 0; k < bytes; k++) {
|
|---|
| 86 | if (((uint8_t *) PA2KA(frames[i]))[k] != val) {
|
|---|
| 87 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 88 | "Unexpected data (%c) in block %zu offset %zu\n",
|
|---|
| 89 | THREAD->tid, CPU->id, ((char *) PA2KA(frames[i]))[k],
|
|---|
| 90 | frames[i], k);
|
|---|
| 91 | atomic_inc(&thread_fail);
|
|---|
| 92 | goto cleanup;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | frame_free(frames[i], count);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | TPRINTF("Thread #%" PRIu64 " (cpu%u): "
|
|---|
| 99 | "Finished run.\n", THREAD->tid, CPU->id);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | cleanup:
|
|---|
| 104 | free(frames);
|
|---|
| 105 |
|
|---|
| 106 | TPRINTF("Thread #%" PRIu64 " (cpu%u): Exiting\n",
|
|---|
| 107 | THREAD->tid, CPU->id);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | const char *test_falloc2(void)
|
|---|
| 111 | {
|
|---|
| 112 | atomic_store(&thread_fail, 0);
|
|---|
| 113 |
|
|---|
| 114 | thread_t *threads[THREADS] = { };
|
|---|
| 115 |
|
|---|
| 116 | for (unsigned int i = 0; i < THREADS; i++) {
|
|---|
| 117 | thread_t *thrd = thread_create(falloc, NULL, TASK,
|
|---|
| 118 | THREAD_FLAG_NONE, "falloc2");
|
|---|
| 119 | if (!thrd) {
|
|---|
| 120 | TPRINTF("Could not create thread %u\n", i);
|
|---|
| 121 | break;
|
|---|
| 122 | }
|
|---|
| 123 | thread_start(thrd);
|
|---|
| 124 | threads[i] = thrd;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | for (unsigned int i = 0; i < THREADS; i++) {
|
|---|
| 128 | if (threads[i] != NULL)
|
|---|
| 129 | thread_join(threads[i]);
|
|---|
| 130 |
|
|---|
| 131 | TPRINTF("Threads left: %u\n", THREADS - i - 1);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | if (atomic_load(&thread_fail) == 0)
|
|---|
| 135 | return NULL;
|
|---|
| 136 |
|
|---|
| 137 | return "Test failed";
|
|---|
| 138 | }
|
|---|