00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <stdio.h>
00030 #include <unistd.h>
00031 #include <stdlib.h>
00032 #include <malloc.h>
00033 #include <as.h>
00034 #include <errno.h>
00035 #include "../tester.h"
00036
00037 #define BUFFER1_PAGES 4
00038 #define BUFFER2_PAGES 2
00039
00040 static void *create_as_area(size_t size)
00041 {
00042 void *result = as_get_mappable_page(size);
00043 TPRINTF("Creating AS area...\n");
00044 if (as_area_create(result, size,
00045 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE) != result) {
00046 return NULL;
00047 }
00048 return result;
00049 }
00050
00051 static void touch_area(void *area, size_t size)
00052 {
00053 TPRINTF("Touching (faulting-in) AS area...\n");
00054
00055 char *ptr = (char *)area;
00056
00057 while (size > 0) {
00058 *ptr = 0;
00059 size--;
00060 ptr++;
00061 }
00062 }
00063
00064 #define VERIFY_MAPPING(area, page_count, expected_rc) \
00065 verify_mapping((area), (page_count), (expected_rc), #expected_rc)
00066
00067 static bool verify_mapping(void *area, int page_count, int expected_rc,
00068 const char *expected_rc_str)
00069 {
00070 TPRINTF("Verifying mapping (expected: %s).\n", expected_rc_str);
00071 int i;
00072 for (i = 0; i < page_count; i++) {
00073 void *page_start = ((char *)area) + PAGE_SIZE * i;
00074 int rc = as_get_physical_mapping(page_start, NULL);
00075 if (rc != expected_rc) {
00076 TPRINTF("as_get_physical_mapping() = %d != %d\n",
00077 rc, expected_rc);
00078 return false;
00079 }
00080 }
00081 return true;
00082 }
00083
00084 const char *test_mapping1(void)
00085 {
00086 int rc;
00087
00088 size_t buffer1_len = BUFFER1_PAGES * PAGE_SIZE;
00089 size_t buffer2_len = BUFFER2_PAGES * PAGE_SIZE;
00090 void *buffer1 = create_as_area(buffer1_len);
00091 void *buffer2 = create_as_area(buffer2_len);
00092 if (!buffer1 || !buffer2) {
00093 return "Cannot allocate memory";
00094 }
00095
00096 touch_area(buffer1, buffer1_len);
00097 touch_area(buffer2, buffer2_len);
00098
00099
00100 if (!VERIFY_MAPPING(buffer1, BUFFER1_PAGES, EOK)) {
00101 return "Failed to find mapping (buffer1)";
00102 }
00103 if (!VERIFY_MAPPING(buffer2, BUFFER2_PAGES, EOK)) {
00104 return "Failed to find mapping (buffer2)";
00105 }
00106
00107
00108 rc = as_area_destroy(buffer1);
00109 if (rc != EOK) {
00110 return "Failed to destroy AS area";
00111 }
00112 if (!VERIFY_MAPPING(buffer1, BUFFER1_PAGES, ENOENT)) {
00113 return "Mapping of destroyed area still exists";
00114 }
00115
00116
00117 rc = as_area_destroy(buffer2);
00118 if (rc != EOK) {
00119 return "Failed to destroy AS area";
00120 }
00121
00122 return NULL;
00123 }