int run(void) { printf("%s: starting...\n", get_instance_name()); l_lock(); printf("%s: got lock!\n", get_instance_name()); l_unlock(); printf("%s: released\n", get_instance_name()); return 0; }
void a_switch_to(void) { static bool not_first; while (not_first); printf("%s: Received call from Receiver\n", get_instance_name()); /* OK, now let's proceed to try to trash our reply cap. You can more or * less read execution in a straight line from here as each function calls * the next. */ printf("%s: Acquiring mutex...\n", get_instance_name()); m_lock(); printf("%s: Acquiring semaphore...\n", get_instance_name()); s_wait(); /* Trigger some more calls on the original endpoint, just in case this is * not correctly handled. */ p1_emit(); p2_emit(); printf("%s: Taking first hop...\n", get_instance_name()); b1_switch_to(); printf("%s: Taking second hop...\n", get_instance_name()); c1_switch_to(); printf("%s: Taking third hop...\n", get_instance_name()); d1_switch_to(); printf("%s: Taking fourth (external) hop...\n", get_instance_name()); e_switch_to(); printf("%s: Releasing mutex...\n", get_instance_name()); m_unlock(); printf("%s: Releasing semaphore...\n", get_instance_name()); s_post(); printf("%s: Returning (moment of truth)...\n", get_instance_name()); /* Prevent future output that can confuse users. */ if (!not_first) { not_first = true; } }
/* Handle a DHCPDISCOVER message. */ static uint32_t discover(uint64_t hwaddr, uint32_t *siaddr) { lock_lock(); /* Figure out a suitable IP address to offer them */ uint32_t offer; do { offer = (my_ip & ~0xff) | (uint32_t)next_offer_octet; next_offer_octet++; } while ((offer & 0xff) == 0 || offer == my_ip || offer == routing_table[0] || offer == routing_table[1] || offer == routing_table[2] || offer == routing_table[3]); lock_unlock(); /* Pass them our IP address so they can pass it back to us when requested * IP assignment. This thing is in the DHCP spec to allow for multiple DHCP * servers on the same network. */ *siaddr = my_ip; char pretty_ip[STRLEN_IP]; ip_to_string(offer, pretty_ip); dprintf("%s: Sending DHCPOFFER of IP %s\n", get_instance_name(), pretty_ip); return offer; }
int run(void) { uart_put("hello from "); uart_put(get_instance_name()); uart_put_char('\n'); h_handoff(); return 0; }
int run(void) { int operands[] = { 342, 74, 283, 37, 534 }; int sz = sizeof(operands) / sizeof(int); const char *name = get_instance_name(); printf("%s: what's the answer to ", name); for (int i = 0; i < sz; i++) { printf("%d ", operands[i]); if (i != sz - 1) { printf("+ "); } } printf("?\n"); struct payload *p = (void*)d + 1024; p->sz = sz; for (int i = 0; i < sz; i++) { p->operands[i] = operands[i]; } dataport_ptr_t ptr = a_calculate(dataport_wrap_ptr((void*)p)); p = dataport_unwrap_ptr(ptr); printf("%s: result was %d\n", name, p->result); return 0; }
int run(void) { const char *name = get_instance_name(); printf("%s: Waiting for client init...\n", name); setup_wait(); printf("%s: Initialising lock...\n", name); sync_spinlock_init((sync_spinlock_t*)&outgoing->lock); printf("%s: Notifying receiver...\n", name); init_emit(); printf("%s: Waiting for data...\n", name); int received = 0; while (!received) { sync_spinlock_lock((sync_spinlock_t*)&incoming->lock); if (incoming->full) { sync_spinlock_lock((sync_spinlock_t*)&outgoing->lock); strcpy((char*)outgoing->data, (char*)incoming->data); outgoing->full = 1; sync_spinlock_unlock((sync_spinlock_t*)&outgoing->lock); received = 1; } sync_spinlock_unlock((sync_spinlock_t*)&incoming->lock); } printf("%s: Done.\n", name); return 0; }
/* Handle a DHCPREQ message */ static uint32_t request(unsigned int client, uint32_t ip, uint32_t siaddr) { if (siaddr != my_ip) { /* This message was intended for a different DHCP server. In a real * system we wouldn't send a DHCPNAK here, but would just ignore this * message. In this simulated setup we are the only server and we * *must* send a reply, so we just NAK it. */ dprintf("%s: Sending DHCPNAK due to server IP mismatch\n", get_instance_name()); return 0; } assert(client < routing_table_sz && "DHCPREQ from non-existent client"); lock_lock(); /* IP address the client gets. This will remain 0 after the logic below if * we're denying the client's request. */ uint32_t assigned = 0; if (routing_table[client] == ip) { /* They requested their existing IP. OK, whatever. */ dprintf("%s: Sending DHCPACK to client %u for its existing IP\n", get_instance_name(), client); assigned = ip; } else if (ip != my_ip && ip != 0 && ip != routing_table[0] && ip != routing_table[1] && ip != routing_table[2] && ip != routing_table[3]) { /* They requested an IP that was not ours, 0 or in the routing table * already. XXX: we should probably block the broadcast IP here too. */ char pretty_ip[STRLEN_IP]; ip_to_string(ip, pretty_ip); dprintf("%s: Sending DHCPACK to client %u of IP %s\n", get_instance_name(), client, pretty_ip); routing_table[client] = ip; assigned = ip; } lock_unlock(); return assigned; }
void handle_picoserver_notification(void) { picoserver_event_t server_event = echo_control_event_poll(); int ret = 0; int socket = 0; uint16_t events = 0; char ip_string[16] = {0}; while (server_event.num_events_left > 0 || server_event.events) { socket = server_event.socket_fd; events = server_event.events; if (events & PICOSERVER_CONN) { if (socket != listener_socket) { picoserver_peer_t peer = echo_control_accept(socket); if (peer.result == -1) { assert(!"Failed to accept a peer"); } pico_ipv4_to_string(ip_string, peer.peer_addr); printf("%s: Connection established with %s on socket %d\n", get_instance_name(), ip_string, socket); } } if (events & PICOSERVER_READ) { printf("%s: Received a message on socket %d, going to echo to Listener\n", get_instance_name(), socket); ret = echo_recv_recv(socket, 4096, 0); strncpy(echo_send_buf, echo_recv_buf, ret); ret = echo_send_send(listener_socket, strlen(echo_send_buf), 0); memset(echo_recv_buf, 0, 4096); memset(echo_send_buf, 0, 4096); } if (events & PICOSERVER_CLOSE) { ret = echo_control_shutdown(socket, PICOSERVER_SHUT_RDWR); printf("%s: Connection closing on socket %d\n", get_instance_name(), socket); } if (events & PICOSERVER_FIN) { printf("%s: Connection closed on socket %d\n", get_instance_name(), socket); } if (events & PICOSERVER_ERR) { printf("%s: Error with socket %d, going to die\n", get_instance_name(), socket); assert(0); } server_event = echo_control_event_poll(); } }
int run(void) { const char *name = get_instance_name(); printf("%s: Started\n", name); printf("%s: Trying to acquire the lock...\n", name); lock_lock(); printf("%s: Got it!\n", name); printf("%s: Let's do some long running calculation (or more accurately, waste time)...\n", name); for (int i = 0; i < 10000; i++) asm volatile (""); printf("%s: Releasing the lock...\n", name); lock_unlock(); printf("%s: Done; let's spin.\n", name); while (1); return 0; }
int run(void) { const char *s = "hello world"; const char *name = get_instance_name(); printf("%s: Initialising lock...\n", name); sync_spinlock_init((sync_spinlock_t*)&sock->lock); printf("%s: Notifying transport...\n", name); init_emit(); printf("%s: Writing \"%s\"...\n", name, s); sync_spinlock_lock((sync_spinlock_t*)&sock->lock); strcpy((char*)sock->data, s); sock->full = 1; sync_spinlock_unlock((sync_spinlock_t*)&sock->lock); printf("%s: Done.\n", name); return 0; }
int run(void) { const char *name = get_instance_name(); printf("%s: Waiting for transport init...\n", name); setup_wait(); printf("%s: Waiting for data...\n", name); int received = 0; while (!received) { sync_spinlock_lock((sync_spinlock_t*)&sock->lock); if (sock->full) { printf("%s: Received \"%s\".\n", name, sock->data); received = 1; } sync_spinlock_unlock((sync_spinlock_t*)&sock->lock); } printf("%s: Done.\n", name); return 0; }
int run(void) { printf("%s instance starting up, going to be listening on %s:%d\n", get_instance_name(), ip_addr, ECHO_PORT); int socket_in = echo_control_open(false); if (socket_in == -1) { assert(!"Failed to open a socket for listening!"); } listener_socket = echo_control_open(false); if (listener_socket == -1) { assert(!"Failed to open a socket for echoing!"); } int ret = echo_control_bind(socket_in, PICOSERVER_ANY_ADDR_IPV4, ECHO_PORT); if (ret) { assert(!"Failed to bind a socket for listening!"); } ret = echo_control_listen(socket_in, 1); if (ret) { assert(!"Failed to listen for incoming connections!"); } uint32_t ip = 0; pico_string_to_ipv4(ip_addr, &ip); ret = echo_control_connect(listener_socket, ip, LISTENER_PORT); if (ret) { assert(!"Failed to connect to the listener!"); } /* Now poll for events and handle them */ seL4_Word badge; while (1) { seL4_Wait(echo_control_notification(), &badge); handle_picoserver_notification(); } }
int a_calculate(size_t operands_sz, const int *operands, size_t *other_sz, int **other, size_t *inplace_sz, int **inplace) { const char *name = get_instance_name(); int total = 1; for (int i = 0; i < operands_sz; i++) { printf("%s: multiplying %d\n", name, operands[i]); total *= operands[i]; } int i; *other = (int*)malloc(sizeof(int) * *inplace_sz); assert(*other != NULL); for (i = 0; i < *inplace_sz; i++) { printf("%s: stashing %d\n", name, (*inplace)[i]); (*other)[i] = (*inplace)[i]; } *other_sz = i; for (i = 1; i < *inplace_sz; i++) { printf("%s: multiplying %d\n", name, (*inplace)[i]); (*inplace)[0] *= (*inplace)[i]; } *inplace_sz = 1; return total; }
/* hint 1: the name of the function to implement is a composition of an interface name and a function name: * i.e.: <interface>_<function> * hint 2: the interfaces available are defined by the component, e.g. in components/Echo/Echo.camkes * hint 3: the function name is defined by the interface definition, e.g. in interfaces/HelloSimple.idl4 * hint 4: so the function would be: hello_say_hello() * hint 5: the CAmkES 'string' type maps to 'const char *' in C * hint 6: make the function print out a mesage using printf * hint 7: look at https://github.com/seL4/camkes-tool/blob/2.1.0/docs/index.md#creating-an-application */ void hello_say_hello(const char *str) { printf("Component %s saying: %s\n", get_instance_name(), str); }
void h_handoff(void) { uart_put("hello from "); uart_put(get_instance_name()); uart_put_char('\n'); }
int run() { printf("%s: Attempting to cause a stack overflow...\n", get_instance_name()); stack_overflow(); return 0; }