2022-05-08 16:50:29 +00:00
|
|
|
#include "u.h"
|
|
|
|
#include "tos.h"
|
|
|
|
#include "../port/lib.h"
|
|
|
|
#include "mem.h"
|
|
|
|
#include "dat.h"
|
|
|
|
#include "fns.h"
|
|
|
|
#include "../port/error.h"
|
|
|
|
#include "pool.h"
|
|
|
|
#include "io.h"
|
|
|
|
#include "sysreg.h"
|
2022-05-08 20:26:56 +00:00
|
|
|
#include "ureg.h"
|
2022-05-08 16:50:29 +00:00
|
|
|
|
2022-07-10 11:35:58 +00:00
|
|
|
#include "rebootcode.i"
|
|
|
|
|
2022-05-08 16:50:29 +00:00
|
|
|
Conf conf;
|
|
|
|
|
2022-07-10 16:40:36 +00:00
|
|
|
#define MAXCONF 64
|
|
|
|
static char *confname[MAXCONF];
|
|
|
|
static char *confval[MAXCONF];
|
|
|
|
static int nconf;
|
|
|
|
|
|
|
|
void
|
|
|
|
bootargsinit(void)
|
|
|
|
{
|
|
|
|
int i, j, n;
|
|
|
|
char *cp, *line[MAXCONF], *p, *q;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse configuration args from dos file plan9.ini
|
|
|
|
*/
|
|
|
|
cp = BOOTARGS; /* where b.com leaves its config */
|
|
|
|
cp[BOOTARGSLEN-1] = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip out '\r', change '\t' -> ' '.
|
|
|
|
*/
|
|
|
|
p = cp;
|
|
|
|
for(q = cp; *q; q++){
|
|
|
|
if(*q == -1)
|
|
|
|
break;
|
|
|
|
if(*q == '\r')
|
|
|
|
continue;
|
|
|
|
if(*q == '\t')
|
|
|
|
*q = ' ';
|
|
|
|
*p++ = *q;
|
|
|
|
}
|
|
|
|
*p = 0;
|
|
|
|
|
|
|
|
n = getfields(cp, line, MAXCONF, 1, "\n");
|
|
|
|
for(i = 0; i < n; i++){
|
|
|
|
if(*line[i] == '#')
|
|
|
|
continue;
|
|
|
|
cp = strchr(line[i], '=');
|
|
|
|
if(cp == nil)
|
|
|
|
continue;
|
|
|
|
*cp++ = '\0';
|
|
|
|
for(j = 0; j < nconf; j++){
|
|
|
|
if(cistrcmp(confname[j], line[i]) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
confname[j] = line[i];
|
|
|
|
confval[j] = cp;
|
|
|
|
if(j == nconf)
|
|
|
|
nconf++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
getconf(char *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < nconf; i++)
|
|
|
|
if(cistrcmp(confname[i], name) == 0)
|
|
|
|
return confval[i];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
setconfenv(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < nconf; i++){
|
|
|
|
if(confname[i][0] != '*')
|
|
|
|
ksetenv(confname[i], confval[i], 0);
|
|
|
|
ksetenv(confname[i], confval[i], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
writeconf(void)
|
|
|
|
{
|
|
|
|
char *p, *q;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
p = getconfenv();
|
|
|
|
if(waserror()) {
|
|
|
|
free(p);
|
|
|
|
nexterror();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* convert to name=value\n format */
|
|
|
|
for(q=p; *q; q++) {
|
|
|
|
q += strlen(q);
|
|
|
|
*q = '=';
|
|
|
|
q += strlen(q);
|
|
|
|
*q = '\n';
|
|
|
|
}
|
|
|
|
n = q - p + 1;
|
|
|
|
if(n >= BOOTARGSLEN)
|
|
|
|
error("kernel configuration too large");
|
|
|
|
memmove(BOOTARGS, p, n);
|
|
|
|
memset(BOOTARGS+n, 0, BOOTARGSLEN-n);
|
|
|
|
poperror();
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
isaconfig(char *, int, ISAConf *)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-08 16:50:29 +00:00
|
|
|
/*
|
|
|
|
* starting place for first process
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init0(void)
|
|
|
|
{
|
|
|
|
char **sp;
|
|
|
|
|
|
|
|
chandevinit();
|
|
|
|
|
|
|
|
if(!waserror()){
|
|
|
|
ksetenv("cputype", "arm64", 0);
|
|
|
|
if(cpuserver)
|
|
|
|
ksetenv("service", "cpu", 0);
|
|
|
|
else
|
|
|
|
ksetenv("service", "terminal", 0);
|
|
|
|
ksetenv("console", "0", 0);
|
2022-07-10 16:40:36 +00:00
|
|
|
setconfenv();
|
2022-05-08 16:50:29 +00:00
|
|
|
poperror();
|
|
|
|
}
|
|
|
|
kproc("alarm", alarmkproc, 0);
|
2022-06-11 21:12:04 +00:00
|
|
|
|
2022-05-08 16:50:29 +00:00
|
|
|
sp = (char**)(USTKTOP-sizeof(Tos) - 8 - sizeof(sp[0])*4);
|
|
|
|
sp[3] = sp[2] = sp[1] = nil;
|
|
|
|
strcpy(sp[1] = (char*)&sp[4], "boot");
|
|
|
|
sp[0] = (void*)&sp[1];
|
|
|
|
touser((uintptr)sp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
confinit(void)
|
|
|
|
{
|
|
|
|
int userpcnt;
|
|
|
|
ulong kpages;
|
|
|
|
char *p;
|
|
|
|
int i;
|
|
|
|
|
2022-05-08 20:26:56 +00:00
|
|
|
conf.nmach = MAXMACH;
|
2022-05-08 16:50:29 +00:00
|
|
|
|
|
|
|
if(p = getconf("service")){
|
|
|
|
if(strcmp(p, "cpu") == 0)
|
|
|
|
cpuserver = 1;
|
|
|
|
else if(strcmp(p,"terminal") == 0)
|
|
|
|
cpuserver = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(p = getconf("*kernelpercent"))
|
|
|
|
userpcnt = 100 - strtol(p, 0, 0);
|
|
|
|
else
|
|
|
|
userpcnt = 0;
|
|
|
|
|
|
|
|
if(userpcnt < 10)
|
|
|
|
userpcnt = 60 + cpuserver*10;
|
|
|
|
|
|
|
|
conf.npage = 0;
|
|
|
|
for(i = 0; i < nelem(conf.mem); i++)
|
|
|
|
conf.npage += conf.mem[i].npage;
|
|
|
|
|
|
|
|
kpages = conf.npage - (conf.npage*userpcnt)/100;
|
|
|
|
if(kpages > ((uintptr)-VDRAM)/BY2PG)
|
|
|
|
kpages = ((uintptr)-VDRAM)/BY2PG;
|
|
|
|
|
|
|
|
conf.upages = conf.npage - kpages;
|
|
|
|
conf.ialloc = (kpages/2)*BY2PG;
|
|
|
|
|
|
|
|
/* set up other configuration parameters */
|
|
|
|
conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
|
|
|
|
if(cpuserver)
|
|
|
|
conf.nproc *= 3;
|
|
|
|
if(conf.nproc > 2000)
|
|
|
|
conf.nproc = 2000;
|
|
|
|
conf.nswap = conf.npage*3;
|
|
|
|
conf.nswppo = 4096;
|
|
|
|
conf.nimage = 200;
|
|
|
|
|
|
|
|
conf.copymode = conf.nmach > 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Guess how much is taken by the large permanent
|
|
|
|
* datastructures. Mntcache and Mntrpc are not accounted for.
|
|
|
|
*/
|
|
|
|
kpages = conf.npage - conf.upages;
|
|
|
|
kpages *= BY2PG;
|
|
|
|
kpages -= conf.upages*sizeof(Page)
|
|
|
|
+ conf.nproc*sizeof(Proc)
|
|
|
|
+ conf.nimage*sizeof(Image)
|
|
|
|
+ conf.nswap
|
|
|
|
+ conf.nswppo*sizeof(Page*);
|
|
|
|
mainmem->maxsize = kpages;
|
2022-06-11 21:12:04 +00:00
|
|
|
imagmem->maxsize = kpages;
|
2022-05-08 16:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
machinit(void)
|
|
|
|
{
|
|
|
|
m->ticks = 1;
|
|
|
|
m->perf.period = 1;
|
|
|
|
active.machs[m->machno] = 1;
|
|
|
|
}
|
|
|
|
|
2022-05-08 20:26:56 +00:00
|
|
|
void
|
|
|
|
mpinit(void)
|
|
|
|
{
|
|
|
|
extern void _start(void);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
splhi();
|
|
|
|
for(i = 1; i < conf.nmach; i++){
|
|
|
|
Ureg u = {0};
|
|
|
|
|
|
|
|
MACHP(i)->machno = i;
|
|
|
|
cachedwbinvse(MACHP(i), MACHSIZE);
|
|
|
|
|
2022-07-10 11:35:58 +00:00
|
|
|
u.r0 = 0x84000003; /* CPU_ON */
|
2022-05-08 20:26:56 +00:00
|
|
|
u.r1 = (sysrd(MPIDR_EL1) & ~0xFF) | i;
|
|
|
|
u.r2 = PADDR(_start);
|
|
|
|
u.r3 = i;
|
|
|
|
smccall(&u);
|
|
|
|
}
|
|
|
|
synccycles();
|
|
|
|
spllo();
|
|
|
|
}
|
|
|
|
|
2022-07-09 16:06:42 +00:00
|
|
|
void
|
|
|
|
cpuidprint(void)
|
|
|
|
{
|
|
|
|
iprint("cpu%d: %dMHz ARM Cortex A53\n", m->machno, m->cpumhz);
|
|
|
|
}
|
|
|
|
|
2022-05-08 16:50:29 +00:00
|
|
|
void
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
machinit();
|
|
|
|
if(m->machno){
|
|
|
|
trapinit();
|
|
|
|
fpuinit();
|
|
|
|
intrinit();
|
|
|
|
clockinit();
|
2022-07-09 16:06:42 +00:00
|
|
|
cpuidprint();
|
2022-05-08 16:50:29 +00:00
|
|
|
synccycles();
|
|
|
|
timersinit();
|
|
|
|
flushtlb();
|
|
|
|
mmu1init();
|
|
|
|
m->ticks = MACHP(0)->ticks;
|
|
|
|
schedinit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
quotefmtinstall();
|
2022-07-10 16:40:36 +00:00
|
|
|
bootargsinit();
|
2022-05-08 16:50:29 +00:00
|
|
|
meminit();
|
|
|
|
confinit();
|
|
|
|
xinit();
|
|
|
|
uartconsinit();
|
|
|
|
printinit();
|
|
|
|
print("\nPlan 9\n");
|
|
|
|
trapinit();
|
|
|
|
fpuinit();
|
|
|
|
intrinit();
|
|
|
|
clockinit();
|
2022-07-09 16:06:42 +00:00
|
|
|
cpuidprint();
|
2022-05-08 16:50:29 +00:00
|
|
|
timersinit();
|
|
|
|
pageinit();
|
|
|
|
procinit0();
|
|
|
|
initseg();
|
|
|
|
links();
|
|
|
|
chandevreset();
|
2022-06-11 21:12:04 +00:00
|
|
|
lcdinit();
|
2022-05-08 16:50:29 +00:00
|
|
|
userinit();
|
2022-05-08 20:26:56 +00:00
|
|
|
mpinit();
|
2022-05-08 16:50:29 +00:00
|
|
|
mmu0clear((uintptr*)L1);
|
|
|
|
flushtlb();
|
|
|
|
mmu1init();
|
|
|
|
schedinit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
exit(int)
|
|
|
|
{
|
2022-07-10 11:35:58 +00:00
|
|
|
Ureg u = { .r0 = 0x84000002 }; /* CPU_OFF */
|
2022-05-08 20:26:56 +00:00
|
|
|
|
2022-05-08 16:50:29 +00:00
|
|
|
cpushutdown();
|
|
|
|
splfhi();
|
2022-05-08 20:26:56 +00:00
|
|
|
|
2022-07-10 11:35:58 +00:00
|
|
|
if(m->machno == 0)
|
|
|
|
u.r0 = 0x84000009; /* SYSTEM RESET */
|
2022-05-08 20:26:56 +00:00
|
|
|
smccall(&u);
|
2022-05-08 16:50:29 +00:00
|
|
|
}
|
|
|
|
|
2022-07-10 11:35:58 +00:00
|
|
|
static void
|
|
|
|
rebootjump(void *entry, void *code, ulong size)
|
|
|
|
{
|
|
|
|
void (*f)(void*, void*, ulong);
|
|
|
|
|
|
|
|
intrcpushutdown();
|
|
|
|
|
|
|
|
/* redo identity map */
|
|
|
|
mmuidmap((uintptr*)L1);
|
|
|
|
|
|
|
|
/* setup reboot trampoline function */
|
|
|
|
f = (void*)REBOOTADDR;
|
|
|
|
memmove(f, rebootcode, sizeof(rebootcode));
|
|
|
|
|
|
|
|
cachedwbinvse(f, sizeof(rebootcode));
|
|
|
|
cacheiinvse(f, sizeof(rebootcode));
|
|
|
|
|
|
|
|
(*f)(entry, code, size);
|
|
|
|
|
|
|
|
for(;;);
|
|
|
|
}
|
|
|
|
|
2022-05-08 16:50:29 +00:00
|
|
|
void
|
2022-07-10 11:35:58 +00:00
|
|
|
reboot(void*, void *code, ulong size)
|
2022-05-08 16:50:29 +00:00
|
|
|
{
|
2022-07-10 11:35:58 +00:00
|
|
|
writeconf();
|
|
|
|
while(m->machno != 0){
|
|
|
|
procwired(up, 0);
|
|
|
|
sched();
|
|
|
|
}
|
|
|
|
|
|
|
|
cpushutdown();
|
|
|
|
delay(2000);
|
|
|
|
|
|
|
|
splfhi();
|
|
|
|
|
|
|
|
/* turn off buffered serial console */
|
|
|
|
serialoq = nil;
|
|
|
|
|
|
|
|
/* shutdown devices */
|
|
|
|
chandevshutdown();
|
|
|
|
|
|
|
|
/* stop the clock */
|
|
|
|
clockshutdown();
|
|
|
|
intrsoff();
|
|
|
|
|
|
|
|
/* off we go - never to return */
|
|
|
|
rebootjump((void*)(KTZERO-KZERO), code, size);
|
2022-05-08 16:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dmaflush(int clean, void *p, ulong len)
|
|
|
|
{
|
|
|
|
uintptr s = (uintptr)p;
|
|
|
|
uintptr e = (uintptr)p + len;
|
|
|
|
|
|
|
|
if(clean){
|
|
|
|
s &= ~(BLOCKALIGN-1);
|
|
|
|
e += BLOCKALIGN-1;
|
|
|
|
e &= ~(BLOCKALIGN-1);
|
|
|
|
cachedwbse((void*)s, e - s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(s & BLOCKALIGN-1){
|
|
|
|
s &= ~(BLOCKALIGN-1);
|
|
|
|
cachedwbinvse((void*)s, BLOCKALIGN);
|
|
|
|
s += BLOCKALIGN;
|
|
|
|
}
|
|
|
|
if(e & BLOCKALIGN-1){
|
|
|
|
e &= ~(BLOCKALIGN-1);
|
|
|
|
if(e < s)
|
|
|
|
return;
|
|
|
|
cachedwbinvse((void*)e, BLOCKALIGN);
|
|
|
|
}
|
|
|
|
if(s < e)
|
|
|
|
cachedinvse((void*)s, e - s);
|
|
|
|
}
|