metalsvm/kernel/init.c

286 lines
7.6 KiB
C

/*
* Copyright 2010 Stefan Lankes, Chair for Operating Systems,
* RWTH Aachen University
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is part of MetalSVM.
*/
#include <metalsvm/stddef.h>
#include <metalsvm/stdio.h>
#include <metalsvm/stdlib.h>
#include <metalsvm/string.h>
#include <metalsvm/processor.h>
#include <metalsvm/time.h>
#include <metalsvm/tasks.h>
#include <metalsvm/errno.h>
#include <metalsvm/init.h>
#include <metalsvm/fs.h>
#ifdef CONFIG_MULTIBOOT
#include <asm/multiboot.h>
#endif
#ifdef CONFIG_LWIP
#include <lwip/init.h>
#include <lwip/sys.h>
#include <lwip/stats.h>
#include <lwip/udp.h>
#include <lwip/tcp.h>
#include <lwip/tcpip.h>
#include <lwip/dhcp.h>
#include <lwip/netifapi.h>
#include <lwip/timers.h>
#include <netif/etharp.h>
#endif
#include <net/rtl8139.h>
#include <net/e1000.h>
#include <net/rckemac.h>
#include <net/mmnif.h>
#ifdef CONFIG_ROCKCREEK
#include <asm/RCCE.h>
#include <asm/RCCE_lib.h>
#endif
int test_init(void);
/*
* Note that linker symbols are not variables, they have no memory allocated for
* maintaining a value, rather their address is their value.
*/
extern const void bss_start;
extern const void bss_end;
int lowlevel_init(void)
{
// initialize .bss section
memset((char*) &bss_start, 0x00, (char*) &bss_end - (char*) &bss_start);
koutput_init();
//kprintf("Now, the BSS section (0x%x - 0x%x) is initialized.\n", (size_t) &bss_start, (size_t) &bss_end);
#ifdef CONFIG_MULTIBOOT
//kprintf("Start kernel with command line \"%s\"\n", mb_info->cmdline);
#endif
return 0;
}
#if defined(CONFIG_LWIP) && (defined(CONFIG_PCI) || defined(CONFIG_ROCKCREEK))
static struct netif default_netif;
#ifdef CONFIG_ROCKCREEK
static struct netif mmnif_netif;
#endif
static int init_netifs(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
#if !NO_SYS
err_t err;
#endif
kputs("Initialize NICs...\n");
// Set up the lwIP network interface
memset(&default_netif, 0x00, sizeof(struct netif));
#ifdef CONFIG_ROCKCREEK
memset(&mmnif_netif, 0x00, sizeof(struct netif));
/* Set network address variables */
IP4_ADDR(&gw, 192,168,28,254);
IP4_ADDR(&ipaddr, 192,168,28,RCCE_ue()+1);
IP4_ADDR(&netmask, 255,255,255,0);
/* Bring up the network interface */
#if NO_SYS
netif_add(&default_netif, &ipaddr, &netmask, &gw, NULL, rckemacif_init, ethernet_input);
netif_set_default(&default_netif);
netif_set_up(&default_netif);
#else
/* Note: Our drivers guarantee that the input function will be called in the context of the tcpip thread.
* => Therefore, we are able to use ethernet_input instead of tcpip_input */
if ((err = netifapi_netif_add(&default_netif, &ipaddr, &netmask, &gw, NULL, rckemacif_init, ethernet_input)) != ERR_OK) {
kprintf("Unable to add the network interface: err = %d\n", err);
return -ENODEV;
}
netifapi_netif_set_default(&default_netif);
netifapi_netif_set_up(&default_netif);
#endif
/* Bring up the intra network interface */
struct ip_addr intra_ipaddr;
struct ip_addr intra_netmask;
struct ip_addr intra_gw;
IP4_ADDR(&intra_gw, 0,0,0,0);
IP4_ADDR(&intra_ipaddr, 192,168,0,RCCE_ue()+1);
IP4_ADDR(&intra_netmask, 255,255,255,0);
/* register our Memory Mapped Virtual IP interface in the lwip stack
* and tell him how to use the interface:
* - mmnif_dev : the device data storage
* - ipaddr : the ip address wich should be used
* - gw : the gateway wicht should be used
* - mmnif_init : the initialization which has to be done in order to use our interface
* - ip_input : tells him that he should use ip_input
*/
#if NO_SYS
netif_add(&mmnif_netif, &intra_ipaddr, &intra_netmask, &intra_gw, NULL, mmnif_init, ip_input);
/* tell lwip all initialization is done and we want to set it ab */
netif_set_up(&mmnif_netif);
#else
/* Note: Our drivers guarantee that the input function will be called in the context of the tcpip thread.
* => Therefore, we are able to use ip_input instead of tcpip_input */
if ((err = netifapi_netif_add(&mmnif_netif, &intra_ipaddr, &intra_netmask, &intra_gw, NULL, mmnif_init, ip_input)) != ERR_OK)
{
kprintf("Unable to add the intra network interface: err = %d\n", err);
return -ENODEV;
}
/* tell lwip all initialization is done and we want to set it ab */
netifapi_netif_set_up(&mmnif_netif);
#endif
#else
/* Clear network address because we use DHCP to get an ip address */
IP4_ADDR(&gw, 0,0,0,0);
IP4_ADDR(&ipaddr, 0,0,0,0);
IP4_ADDR(&netmask, 0,0,0,0);
/* Bring up the network interface */
#if NO_SYS
uint32_t flags = irq_nested_disable();
netif_add(&default_netif, &ipaddr, &netmask, &gw, NULL, rtl8139if_init, ethernet_input);
netif_set_default(&default_netif);
#else
/* Note: Our drivers guarantee that the input function will be called in the context of the tcpip thread.
* => Therefore, we are able to use ethernet_input instead of tcpip_input */
if ((err = netifapi_netif_add(&default_netif, &ipaddr, &netmask, &gw, NULL, rtl8139if_init, ethernet_input)) == ERR_OK)
goto success;
if ((err = netifapi_netif_add(&default_netif, &ipaddr, &netmask, &gw, NULL, e1000if_init, ethernet_input)) == ERR_OK)
goto success;
kprintf("Unable to add the network interface: err = %d\n", err);
return -ENODEV;
success:
netifapi_netif_set_default(&default_netif);
#endif
kprintf("Starting DHCPCD...\n");
#if NO_SYS
dhcp_start(&default_netif);
irq_nested_enable(flags);
#else
netifapi_dhcp_start(&default_netif);
#endif
int mscnt = 0;
/* wait for ip address */
while(!default_netif.ip_addr.addr) {
sys_msleep(DHCP_FINE_TIMER_MSECS);
dhcp_fine_tmr();
mscnt += DHCP_FINE_TIMER_MSECS;
if (mscnt >= DHCP_COARSE_TIMER_SECS*1000) {
dhcp_coarse_tmr();
mscnt = 0;
}
}
#endif
return 0;
}
#endif
#ifdef CONFIG_LWIP
#if !NO_SYS
static void tcpip_init_done(void* arg)
{
sys_sem_t* sem = (sys_sem_t*)arg;
kprintf("LwIP's tcpip thread has task id %d\n", per_core(current_task)->id);
sys_sem_signal(sem);
}
#endif
#endif
int network_shutdown(void)
{
#if defined(CONFIG_LWIP) && defined(CONFIG_ROCKCREEK)
mmnif_shutdown();
#if NO_SYS
netif_set_down(&default_netif);
#else
netifapi_netif_set_down(&default_netif);
#endif
#elif defined(CONFIG_LWIP) && defined(CONFIG_PCI)
#if NO_SYS
dhcp_stop(&default_netif);
#else
netifapi_dhcp_stop(&default_netif);
#endif
#endif
return 0;
}
int initd(void* arg)
{
#ifdef CONFIG_LWIP
#if !NO_SYS
sys_sem_t sem;
#endif
tid_t id;
char* argv[] = {"/bin/rlogind ", NULL};
// Initialize lwIP modules
#if NO_SYS
lwip_init();
#else
if(sys_sem_new(&sem, 0) != ERR_OK)
LWIP_ASSERT("Failed to create semaphore", 0);
tcpip_init(tcpip_init_done, &sem);
sys_sem_wait(&sem);
kprintf("TCP/IP initialized.\n");
sys_sem_free(&sem);
#endif
#if defined(CONFIG_LWIP) && (defined(CONFIG_PCI) || defined(CONFIG_ROCKCREEK))
init_netifs();
/* test if interface is really up */
if (!netif_is_up(&default_netif))
kputs("network interface is not up\n");
#endif
#if !NO_SYS && defined(CONFIG_X86_32)
create_user_task(&id, "/bin/rlogind", argv);
kprintf("Create rlogind with id %u\n", id);
#endif
#endif
#if 1
kputs("Filesystem:\n");
list_fs(fs_root, 1);
#endif
test_init();
return 0;
}