mapping1.c

00001 /*
00002  * Copyright (c) 2011 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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         /* Now verify that mapping to physical frames exist. */
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         /* Let's destroy the buffer1 area and access it again. */
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         /* clean-up */
00117         rc = as_area_destroy(buffer2);
00118         if (rc != EOK) {
00119                 return "Failed to destroy AS area";
00120         }
00121         
00122         return NULL;
00123 }

Generated on Thu Jun 2 07:45:43 2011 for HelenOS/USB by  doxygen 1.4.7