OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [test0/] [src/] [ipctest.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Tests for more complex ipc such as full and extended
3
 *
4
 * Copyright (C) 2007-2009 Bahadir Bilgehan Balban
5
 */
6
#include <l4lib/ipcdefs.h>
7
#include <errno.h>
8
#include <sys/mman.h>
9
#include <sys/types.h>
10
#include <unistd.h>
11
#include <tests.h>
12
#include <capability.h>
13
 
14
/*
15
 * Full ipc test. Sends/receives full utcb, done with the pager.
16
 */
17
void ipc_full_test(void)
18
{
19
        int ret = 0;
20
 
21
        /* Fill in all of the utcb locations */
22
        for (int i = MR_UNUSED_START; i < MR_TOTAL + MR_REST; i++) {
23
                //printf("Writing: MR%d: %d\n", i, i);
24
                write_mr(i, i);
25
        }
26
 
27
        /* Call the pager */
28
        if ((ret = l4_sendrecv_full(pagerid, pagerid,
29
                                    L4_IPC_TAG_SYNC_FULL)) < 0) {
30
                printf("%s: Failed with %d\n", __FUNCTION__, ret);
31
                BUG();
32
        }
33
        /* Read back updated utcb */
34
        for (int i = MR_UNUSED_START; i < MR_TOTAL + MR_REST; i++) {
35
                //printf("Read MR%d: %d\n", i, read_mr(i));
36
                if (read_mr(i) != 0) {
37
                        printf("Expected 0 on all mrs. Failed.\n");
38
                        BUG();
39
                }
40
        }
41
 
42
        if (!ret)
43
                printf("FULL IPC TEST:      -- PASSED --\n");
44
        else
45
                printf("FULL IPC TEST:      -- FAILED --\n");
46
}
47
 
48
/*
49
 * This is an extended ipc test that is done between 2 tasks that fork.
50
 */
51
void ipc_extended_test(void)
52
{
53
        pid_t child, parent;
54
        struct capability cap;
55
        void *base;
56
        char *ipcbuf;
57
        int err;
58
 
59
        memset(&cap, 0, sizeof(cap));
60
 
61
        /* Get parent pid */
62
        parent = getpid();
63
 
64
        /* Fork the current task */
65
        if ((child = fork()) < 0) {
66
                test_printf("%s: Fork failed with %d\n", __FUNCTION__, errno);
67
                goto out_err;
68
        }
69
        if (child)
70
                test_printf("%d: Created child with pid %d\n", getpid(), child);
71
        else
72
                test_printf("Child %d running.\n", getpid());
73
 
74
        /* This test makes this assumption */
75
        BUG_ON(L4_IPC_EXTENDED_MAX_SIZE > PAGE_SIZE);
76
 
77
        /*
78
         * Request capability to ipc to each other from pager
79
         * (Actually only the sender needs it)
80
         */
81
        if (child) {
82
                cap.owner = parent;
83
                cap.resid = child;
84
        } else {
85
                cap.owner = getpid();
86
                cap.resid = parent;
87
        }
88
        cap.type = CAP_TYPE_IPC | CAP_RTYPE_THREAD;
89
        cap.access = CAP_IPC_EXTENDED | CAP_IPC_SEND | CAP_IPC_RECV;
90
        if ((err = cap_request_pager(&cap)) < 0) {
91
                printf("Ipc capability request failed. "
92
                       "err = %d\n", err);
93
                goto out_err;
94
        }
95
 
96
        /*
97
         * Both child and parent gets 2 pages
98
         * of privately mapped anonymous memory
99
         */
100
        if ((int)(base = mmap(0, PAGE_SIZE*2, PROT_READ | PROT_WRITE,
101
                              MAP_PRIVATE | MAP_ANONYMOUS, 0, 0)) < 0) {
102
                printf("%s: mmap for extended ipc buffer failed: %d\n",
103
                            __FUNCTION__, (int)base);
104
                goto out_err;
105
        } else
106
                test_printf("%s: mmap: Anonymous private buffer at %p\n",
107
                            __FUNCTION__, base);
108
 
109
        /*
110
         * Both tasks read/write both pages
111
         * across the page boundary for messages
112
         */
113
        ipcbuf = base + PAGE_SIZE - L4_IPC_EXTENDED_MAX_SIZE / 2;
114
 
115
        /* Child sending message */
116
        if (child == 0) {
117
                /* Child creates a string of maximum extended ipc size */
118
                for (int i = 0; i < L4_IPC_EXTENDED_MAX_SIZE; i++) {
119
                        ipcbuf[i] = 'A' + i % 20;
120
                }
121
                /* Finish string with newline and null pointer */
122
                ipcbuf[L4_IPC_EXTENDED_MAX_SIZE - 2] = '\n';
123
                ipcbuf[L4_IPC_EXTENDED_MAX_SIZE - 1] = '\0';
124
 
125
                /* Send message to parent */
126
                test_printf("Child sending message: %s\n", ipcbuf);
127
                if ((err = l4_send_extended(parent, L4_IPC_TAG_SYNC_EXTENDED,
128
                                            L4_IPC_EXTENDED_MAX_SIZE,
129
                                            ipcbuf)) < 0) {
130
                        printf("Extended ipc error: %d\n", err);
131
                        goto out_err;
132
                }
133
 
134
        /* Parent receiving message */
135
        } else {
136
                /*
137
                 * Parent receives on the buffer - we are sure that the
138
                 * buffer is not paged in because we did not touch it.
139
                 * This should prove that page faults are handled during
140
                 * the ipc.
141
                 */
142
                test_printf("Parent: extended receiving from child.\n");
143
                if ((err = l4_receive_extended(child, L4_IPC_EXTENDED_MAX_SIZE,
144
                                               ipcbuf)) < 0) {
145
                        printf("Extended ipc error: %d\n", err);
146
                        goto out_err;
147
                }
148
 
149
                /* We now print the message created by child. */
150
                test_printf("(%d): Message received from child: %s\n",
151
                       getpid(), ipcbuf);
152
 
153
                /* Check that string received from child is an exact match */
154
                for (int i = 0; i < L4_IPC_EXTENDED_MAX_SIZE - 2; i++) {
155
                        if (ipcbuf[i] != ('A' + i % 20))
156
                                goto out_err;
157
                }
158
                if (ipcbuf[L4_IPC_EXTENDED_MAX_SIZE - 2] != '\n')
159
                        goto out_err;
160
                if (ipcbuf[L4_IPC_EXTENDED_MAX_SIZE - 1] != '\0')
161
                        goto out_err;
162
 
163
                printf("EXTENDED IPC TEST:  -- PASSED --\n");
164
        }
165
        return;
166
 
167
out_err:
168
        printf("EXTENDED IPC TEST:  -- FAILED --\n");
169
}
170
 
171
 
172
 
173
 
174
 

powered by: WebSVN 2.1.0

© copyright 1999-2026 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.