Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8de6f45af1 | ||
|
|
dc9153fcb9 | ||
|
|
4d7493893d | ||
|
|
0c477a6c84 | ||
|
|
7d47335b4f |
5
Makefile
5
Makefile
@ -188,6 +188,11 @@ UPROGS=\
|
|||||||
$U/_grind\
|
$U/_grind\
|
||||||
$U/_wc\
|
$U/_wc\
|
||||||
$U/_zombie\
|
$U/_zombie\
|
||||||
|
$U/_sleep\
|
||||||
|
$U/_pingpong\
|
||||||
|
$U/_primes\
|
||||||
|
$U/_find\
|
||||||
|
$U/_xargs\
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
36
README
36
README
@ -6,7 +6,7 @@ ACKNOWLEDGMENTS
|
|||||||
|
|
||||||
xv6 is inspired by John Lions's Commentary on UNIX 6th Edition (Peer
|
xv6 is inspired by John Lions's Commentary on UNIX 6th Edition (Peer
|
||||||
to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14,
|
to Peer Communications; ISBN: 1-57398-013-7; 1st edition (June 14,
|
||||||
2000)). See also https://pdos.csail.mit.edu/6.1810/, which provides
|
2000)). See also https://pdos.csail.mit.edu/6.828/, which provides
|
||||||
pointers to on-line resources for v6.
|
pointers to on-line resources for v6.
|
||||||
|
|
||||||
The following people have made contributions: Russ Cox (context switching,
|
The following people have made contributions: Russ Cox (context switching,
|
||||||
@ -14,31 +14,29 @@ locking), Cliff Frey (MP), Xiao Yu (MP), Nickolai Zeldovich, and Austin
|
|||||||
Clements.
|
Clements.
|
||||||
|
|
||||||
We are also grateful for the bug reports and patches contributed by
|
We are also grateful for the bug reports and patches contributed by
|
||||||
Takahiro Aoyagi, Silas Boyd-Wickizer, Anton Burtsev, carlclone, Ian
|
Takahiro Aoyagi, Silas Boyd-Wickizer, Anton Burtsev, Ian Chen, Dan
|
||||||
Chen, Dan Cross, Cody Cutler, Mike CAT, Tej Chajed, Asami Doi,
|
Cross, Cody Cutler, Mike CAT, Tej Chajed, Asami Doi, eyalz800, Nelson
|
||||||
eyalz800, Nelson Elhage, Saar Ettinger, Alice Ferrazzi, Nathaniel
|
Elhage, Saar Ettinger, Alice Ferrazzi, Nathaniel Filardo, flespark,
|
||||||
Filardo, flespark, Peter Froehlich, Yakir Goaron, Shivam Handa, Matt
|
Peter Froehlich, Yakir Goaron, Shivam Handa, Matt Harvey, Bryan Henry,
|
||||||
Harvey, Bryan Henry, jaichenhengjie, Jim Huang, Matúš Jókay, John
|
jaichenhengjie, Jim Huang, Matúš Jókay, Alexander Kapshuk, Anders
|
||||||
Jolly, Alexander Kapshuk, Anders Kaseorg, kehao95, Wolfgang Keller,
|
Kaseorg, kehao95, Wolfgang Keller, Jungwoo Kim, Jonathan Kimmitt,
|
||||||
Jungwoo Kim, Jonathan Kimmitt, Eddie Kohler, Vadim Kolontsov, Austin
|
Eddie Kohler, Vadim Kolontsov, Austin Liew, l0stman, Pavan
|
||||||
Liew, l0stman, Pavan Maddamsetti, Imbar Marinescu, Yandong Mao, Matan
|
Maddamsetti, Imbar Marinescu, Yandong Mao, Matan Shabtay, Hitoshi
|
||||||
Shabtay, Hitoshi Mitake, Carmi Merimovich, Mark Morrissey, mtasm, Joel
|
Mitake, Carmi Merimovich, Mark Morrissey, mtasm, Joel Nider,
|
||||||
Nider, Hayato Ohhashi, OptimisticSide, Harry Porter, Greg Price, Jude
|
OptimisticSide, Greg Price, Jude Rich, Ayan Shafqat, Eldar Sehayek,
|
||||||
Rich, segfault, Ayan Shafqat, Eldar Sehayek, Yongming Shen, Fumiya
|
Yongming Shen, Fumiya Shigemitsu, Cam Tenny, tyfkda, Warren Toomey,
|
||||||
Shigemitsu, Cam Tenny, tyfkda, Warren Toomey, Stephen Tu, Rafael Ubal,
|
Stephen Tu, Rafael Ubal, Amane Uehara, Pablo Ventura, Xi Wang, Keiichi
|
||||||
Amane Uehara, Pablo Ventura, Xi Wang, WaheedHafez, Keiichi Watanabe,
|
Watanabe, Nicolas Wolovick, wxdao, Grant Wu, Jindong Zhang, Icenowy
|
||||||
Nicolas Wolovick, wxdao, Grant Wu, Jindong Zhang, Icenowy Zheng,
|
Zheng, ZhUyU1997, and Zou Chang Wei.
|
||||||
ZhUyU1997, and Zou Chang Wei.
|
|
||||||
|
|
||||||
|
|
||||||
The code in the files that constitute xv6 is
|
The code in the files that constitute xv6 is
|
||||||
Copyright 2006-2022 Frans Kaashoek, Robert Morris, and Russ Cox.
|
Copyright 2006-2020 Frans Kaashoek, Robert Morris, and Russ Cox.
|
||||||
|
|
||||||
ERROR REPORTS
|
ERROR REPORTS
|
||||||
|
|
||||||
Please send errors and suggestions to Frans Kaashoek and Robert Morris
|
Please send errors and suggestions to Frans Kaashoek and Robert Morris
|
||||||
(kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching
|
(kaashoek,rtm@mit.edu). The main purpose of xv6 is as a teaching
|
||||||
operating system for MIT's 6.1810, so we are more interested in
|
operating system for MIT's 6.S081, so we are more interested in
|
||||||
simplifications and clarifications than new features.
|
simplifications and clarifications than new features.
|
||||||
|
|
||||||
BUILDING AND RUNNING XV6
|
BUILDING AND RUNNING XV6
|
||||||
|
|||||||
@ -1 +1 @@
|
|||||||
LAB=net
|
LAB=util
|
||||||
|
|||||||
@ -1,43 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import re
|
|
||||||
import subprocess
|
|
||||||
from gradelib import *
|
|
||||||
|
|
||||||
r = Runner(save("xv6.out"))
|
|
||||||
|
|
||||||
@test(0, "running nettests")
|
|
||||||
def test_nettest():
|
|
||||||
server = subprocess.Popen(["make", "server"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
||||||
r.run_qemu(shell_script([
|
|
||||||
'nettests'
|
|
||||||
]), timeout=30)
|
|
||||||
server.terminate()
|
|
||||||
server.communicate()
|
|
||||||
|
|
||||||
@test(40, "nettest: ping", parent=test_nettest)
|
|
||||||
def test_nettest_():
|
|
||||||
r.match('^testing ping: OK$')
|
|
||||||
|
|
||||||
@test(20, "nettest: single process", parent=test_nettest)
|
|
||||||
def test_nettest_():
|
|
||||||
r.match('^testing single-process pings: OK$')
|
|
||||||
|
|
||||||
@test(20, "nettest: multi-process", parent=test_nettest)
|
|
||||||
def test_nettest_fork_test():
|
|
||||||
r.match('^testing multi-process pings: OK$')
|
|
||||||
|
|
||||||
@test(19, "nettest: DNS", parent=test_nettest)
|
|
||||||
def test_nettest_dns_test():
|
|
||||||
r.match('^DNS OK$')
|
|
||||||
|
|
||||||
#@test(10, "answers-net.txt")
|
|
||||||
#def test_answers():
|
|
||||||
# # just a simple sanity check, will be graded manually
|
|
||||||
# check_answers("answers-net.txt")
|
|
||||||
|
|
||||||
@test(1, "time")
|
|
||||||
def test_time():
|
|
||||||
check_time()
|
|
||||||
|
|
||||||
run_tests()
|
|
||||||
86
grade-lab-util
Executable file
86
grade-lab-util
Executable file
@ -0,0 +1,86 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
from gradelib import *
|
||||||
|
|
||||||
|
r = Runner(save("xv6.out"))
|
||||||
|
|
||||||
|
@test(5, "sleep, no arguments")
|
||||||
|
def test_sleep_no_args():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'sleep'
|
||||||
|
]))
|
||||||
|
r.match(no=["exec .* failed", "$ sleep\n$"])
|
||||||
|
|
||||||
|
@test(5, "sleep, returns")
|
||||||
|
def test_sleep_no_args():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'sleep',
|
||||||
|
'echo OK'
|
||||||
|
]))
|
||||||
|
r.match('^OK$', no=["exec .* failed", "$ sleep\n$"])
|
||||||
|
|
||||||
|
@test(10, "sleep, makes syscall")
|
||||||
|
def test_sleep():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'sleep 10',
|
||||||
|
'echo FAIL'
|
||||||
|
]), stop_breakpoint('sys_sleep'))
|
||||||
|
r.match('\\$ sleep 10', no=['FAIL'])
|
||||||
|
|
||||||
|
@test(20, "pingpong")
|
||||||
|
def test_pingpong():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'pingpong', 'echo OK'
|
||||||
|
]))
|
||||||
|
r.match('^\\d+: received ping$', '^\\d+: received pong$', '^OK$')
|
||||||
|
|
||||||
|
@test(20, "primes")
|
||||||
|
def test_primes():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'primes', 'echo OK'
|
||||||
|
]))
|
||||||
|
args = ['prime %d' % i for i in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]]
|
||||||
|
args.append('^OK$')
|
||||||
|
r.match(*args)
|
||||||
|
|
||||||
|
@test(10, "find, in current directory")
|
||||||
|
def test_find_curdir():
|
||||||
|
fn = random_str()
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'echo > %s' % fn,
|
||||||
|
'find . %s' % fn
|
||||||
|
]))
|
||||||
|
r.match('./%s' % fn)
|
||||||
|
|
||||||
|
@test(10, "find, recursive")
|
||||||
|
def test_find_recursive():
|
||||||
|
needle = random_str()
|
||||||
|
dirs = [random_str() for _ in range(3)]
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'mkdir %s' % dirs[0],
|
||||||
|
'echo > %s/%s' % (dirs[0], needle),
|
||||||
|
'mkdir %s/%s' % (dirs[0], dirs[1]),
|
||||||
|
'echo > %s/%s/%s' % (dirs[0], dirs[1], needle),
|
||||||
|
'mkdir %s' % dirs[2],
|
||||||
|
'echo > %s/%s' % (dirs[2], needle),
|
||||||
|
'find . %s' % needle
|
||||||
|
]))
|
||||||
|
r.match('./%s/%s' % (dirs[0], needle),
|
||||||
|
'./%s/%s/%s' % (dirs[0], dirs[1], needle),
|
||||||
|
'./%s/%s' % (dirs[2], needle))
|
||||||
|
|
||||||
|
@test(19, "xargs")
|
||||||
|
def test_xargs():
|
||||||
|
r.run_qemu(shell_script([
|
||||||
|
'sh < xargstest.sh',
|
||||||
|
'echo DONE',
|
||||||
|
], 'DONE'))
|
||||||
|
matches = re.findall("hello", r.qemu.output)
|
||||||
|
assert_equal(len(matches), 3, "Number of appearances of 'hello'")
|
||||||
|
|
||||||
|
@test(1, "time")
|
||||||
|
def test_time():
|
||||||
|
check_time()
|
||||||
|
|
||||||
|
run_tests()
|
||||||
@ -8,10 +8,6 @@ struct spinlock;
|
|||||||
struct sleeplock;
|
struct sleeplock;
|
||||||
struct stat;
|
struct stat;
|
||||||
struct superblock;
|
struct superblock;
|
||||||
#ifdef LAB_NET
|
|
||||||
struct mbuf;
|
|
||||||
struct sock;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// bio.c
|
// bio.c
|
||||||
void binit(void);
|
void binit(void);
|
||||||
@ -121,11 +117,6 @@ void initlock(struct spinlock*, char*);
|
|||||||
void release(struct spinlock*);
|
void release(struct spinlock*);
|
||||||
void push_off(void);
|
void push_off(void);
|
||||||
void pop_off(void);
|
void pop_off(void);
|
||||||
uint64 lockfree_read8(uint64 *addr);
|
|
||||||
int lockfree_read4(int *addr);
|
|
||||||
#ifdef LAB_LOCK
|
|
||||||
void freelock(struct spinlock*);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// sleeplock.c
|
// sleeplock.c
|
||||||
void acquiresleep(struct sleeplock*);
|
void acquiresleep(struct sleeplock*);
|
||||||
@ -196,44 +187,3 @@ void virtio_disk_intr(void);
|
|||||||
|
|
||||||
// number of elements in fixed-size array
|
// number of elements in fixed-size array
|
||||||
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
|
#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef LAB_PGTBL
|
|
||||||
// vmcopyin.c
|
|
||||||
int copyin_new(pagetable_t, char *, uint64, uint64);
|
|
||||||
int copyinstr_new(pagetable_t, char *, uint64, uint64);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// stats.c
|
|
||||||
void statsinit(void);
|
|
||||||
void statsinc(void);
|
|
||||||
|
|
||||||
// sprintf.c
|
|
||||||
int snprintf(char*, int, char*, ...);
|
|
||||||
|
|
||||||
#ifdef KCSAN
|
|
||||||
void kcsaninit();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
// pci.c
|
|
||||||
void pci_init();
|
|
||||||
|
|
||||||
// e1000.c
|
|
||||||
void e1000_init(uint32 *);
|
|
||||||
void e1000_intr(void);
|
|
||||||
int e1000_transmit(struct mbuf*);
|
|
||||||
|
|
||||||
// net.c
|
|
||||||
void net_rx(struct mbuf*);
|
|
||||||
void net_tx_udp(struct mbuf*, uint32, uint16, uint16);
|
|
||||||
|
|
||||||
// sysnet.c
|
|
||||||
void sockinit(void);
|
|
||||||
int sockalloc(struct file **, uint32, uint16, uint16);
|
|
||||||
void sockclose(struct sock *);
|
|
||||||
int sockread(struct sock *, uint64, int);
|
|
||||||
int sockwrite(struct sock *, uint64, int);
|
|
||||||
void sockrecvudp(struct mbuf*, uint32, uint16, uint16);
|
|
||||||
#endif
|
|
||||||
|
|||||||
162
kernel/e1000.c
162
kernel/e1000.c
@ -1,162 +0,0 @@
|
|||||||
#include "types.h"
|
|
||||||
#include "param.h"
|
|
||||||
#include "memlayout.h"
|
|
||||||
#include "riscv.h"
|
|
||||||
#include "spinlock.h"
|
|
||||||
#include "proc.h"
|
|
||||||
#include "defs.h"
|
|
||||||
#include "e1000_dev.h"
|
|
||||||
#include "net.h"
|
|
||||||
|
|
||||||
#define TX_RING_SIZE 16
|
|
||||||
static struct tx_desc tx_ring[TX_RING_SIZE] __attribute__((aligned(16)));
|
|
||||||
static struct mbuf *tx_mbufs[TX_RING_SIZE];
|
|
||||||
|
|
||||||
#define RX_RING_SIZE 16
|
|
||||||
static struct rx_desc rx_ring[RX_RING_SIZE] __attribute__((aligned(16)));
|
|
||||||
static struct mbuf *rx_mbufs[RX_RING_SIZE];
|
|
||||||
|
|
||||||
// remember where the e1000's registers live.
|
|
||||||
static volatile uint32 *regs;
|
|
||||||
|
|
||||||
struct spinlock e1000_lock;
|
|
||||||
|
|
||||||
// called by pci_init().
|
|
||||||
// xregs is the memory address at which the
|
|
||||||
// e1000's registers are mapped.
|
|
||||||
void
|
|
||||||
e1000_init(uint32 *xregs)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
initlock(&e1000_lock, "e1000");
|
|
||||||
|
|
||||||
regs = xregs;
|
|
||||||
|
|
||||||
// Reset the device
|
|
||||||
regs[E1000_IMS] = 0; // disable interrupts
|
|
||||||
regs[E1000_CTL] |= E1000_CTL_RST;
|
|
||||||
regs[E1000_IMS] = 0; // redisable interrupts
|
|
||||||
__sync_synchronize();
|
|
||||||
|
|
||||||
// [E1000 14.5] Transmit initialization
|
|
||||||
memset(tx_ring, 0, sizeof(tx_ring));
|
|
||||||
for (i = 0; i < TX_RING_SIZE; i++) {
|
|
||||||
tx_ring[i].status = E1000_TXD_STAT_DD;
|
|
||||||
tx_mbufs[i] = 0;
|
|
||||||
}
|
|
||||||
regs[E1000_TDBAL] = (uint64) tx_ring;
|
|
||||||
if(sizeof(tx_ring) % 128 != 0)
|
|
||||||
panic("e1000");
|
|
||||||
regs[E1000_TDLEN] = sizeof(tx_ring);
|
|
||||||
regs[E1000_TDH] = regs[E1000_TDT] = 0;
|
|
||||||
|
|
||||||
// [E1000 14.4] Receive initialization
|
|
||||||
memset(rx_ring, 0, sizeof(rx_ring));
|
|
||||||
for (i = 0; i < RX_RING_SIZE; i++) {
|
|
||||||
rx_mbufs[i] = mbufalloc(0);
|
|
||||||
if (!rx_mbufs[i])
|
|
||||||
panic("e1000");
|
|
||||||
rx_ring[i].addr = (uint64) rx_mbufs[i]->head;
|
|
||||||
}
|
|
||||||
regs[E1000_RDBAL] = (uint64) rx_ring;
|
|
||||||
if(sizeof(rx_ring) % 128 != 0)
|
|
||||||
panic("e1000");
|
|
||||||
regs[E1000_RDH] = 0;
|
|
||||||
regs[E1000_RDT] = RX_RING_SIZE - 1;
|
|
||||||
regs[E1000_RDLEN] = sizeof(rx_ring);
|
|
||||||
|
|
||||||
// filter by qemu's MAC address, 52:54:00:12:34:56
|
|
||||||
regs[E1000_RA] = 0x12005452;
|
|
||||||
regs[E1000_RA+1] = 0x5634 | (1<<31);
|
|
||||||
// multicast table
|
|
||||||
for (int i = 0; i < 4096/32; i++)
|
|
||||||
regs[E1000_MTA + i] = 0;
|
|
||||||
|
|
||||||
// transmitter control bits.
|
|
||||||
regs[E1000_TCTL] = E1000_TCTL_EN | // enable
|
|
||||||
E1000_TCTL_PSP | // pad short packets
|
|
||||||
(0x10 << E1000_TCTL_CT_SHIFT) | // collision stuff
|
|
||||||
(0x40 << E1000_TCTL_COLD_SHIFT);
|
|
||||||
regs[E1000_TIPG] = 10 | (8<<10) | (6<<20); // inter-pkt gap
|
|
||||||
|
|
||||||
// receiver control bits.
|
|
||||||
regs[E1000_RCTL] = E1000_RCTL_EN | // enable receiver
|
|
||||||
E1000_RCTL_BAM | // enable broadcast
|
|
||||||
E1000_RCTL_SZ_2048 | // 2048-byte rx buffers
|
|
||||||
E1000_RCTL_SECRC; // strip CRC
|
|
||||||
|
|
||||||
// ask e1000 for receive interrupts.
|
|
||||||
regs[E1000_RDTR] = 0; // interrupt after every received packet (no timer)
|
|
||||||
regs[E1000_RADV] = 0; // interrupt after every packet (no timer)
|
|
||||||
regs[E1000_IMS] = (1 << 7); // RXDW -- Receiver Descriptor Write Back
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
e1000_transmit(struct mbuf *m)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Your code here.
|
|
||||||
//
|
|
||||||
// the mbuf contains an ethernet frame; program it into
|
|
||||||
// the TX descriptor ring so that the e1000 sends it. Stash
|
|
||||||
// a pointer so that it can be freed after sending.
|
|
||||||
//
|
|
||||||
|
|
||||||
acquire(&e1000_lock);
|
|
||||||
int tdt = regs[E1000_TDT];
|
|
||||||
if (!(tx_ring[tdt].status & E1000_TXD_STAT_DD)) {
|
|
||||||
release(&e1000_lock);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (tx_mbufs[tdt]) {
|
|
||||||
mbuffree(tx_mbufs[tdt]);
|
|
||||||
tx_mbufs[tdt] = 0;
|
|
||||||
}
|
|
||||||
tx_mbufs[tdt] = m;
|
|
||||||
tx_ring[tdt].addr = (uint64)m->head;
|
|
||||||
tx_ring[tdt].length = m->len;
|
|
||||||
tx_ring[tdt].cmd = E1000_TXD_CMD_RS | E1000_TXD_CMD_EOP;
|
|
||||||
regs[E1000_TDT] = (tdt + 1) % TX_RING_SIZE;
|
|
||||||
release(&e1000_lock);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
e1000_recv(void)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Your code here.
|
|
||||||
//
|
|
||||||
// Check for packets that have arrived from the e1000
|
|
||||||
// Create and deliver an mbuf for each packet (using net_rx()).
|
|
||||||
//
|
|
||||||
acquire(&e1000_lock);
|
|
||||||
int rdt = regs[E1000_RDT];
|
|
||||||
rdt = (rdt + 1) % RX_RING_SIZE;
|
|
||||||
while (rx_ring[rdt].status & E1000_RXD_STAT_DD)
|
|
||||||
{
|
|
||||||
if (!holding(&e1000_lock)) acquire(&e1000_lock);
|
|
||||||
rx_mbufs[rdt]->len = rx_ring[rdt].length;
|
|
||||||
struct mbuf* m = rx_mbufs[rdt];
|
|
||||||
rx_mbufs[rdt] = mbufalloc(0);
|
|
||||||
if (!rx_mbufs[rdt]) panic("e1000 recv");
|
|
||||||
rx_ring[rdt].addr = (uint64)rx_mbufs[rdt]->head;
|
|
||||||
rx_ring[rdt].status = 0;
|
|
||||||
regs[E1000_RDT] = rdt;
|
|
||||||
rdt = (rdt + 1) % RX_RING_SIZE;
|
|
||||||
release(&e1000_lock);
|
|
||||||
net_rx(m);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
e1000_intr(void)
|
|
||||||
{
|
|
||||||
// tell the e1000 we've seen this interrupt;
|
|
||||||
// without this the e1000 won't raise any
|
|
||||||
// further interrupts.
|
|
||||||
regs[E1000_ICR] = 0xffffffff;
|
|
||||||
|
|
||||||
e1000_recv();
|
|
||||||
}
|
|
||||||
@ -1,125 +0,0 @@
|
|||||||
//
|
|
||||||
// E1000 hardware definitions: registers and DMA ring format.
|
|
||||||
// from the Intel 82540EP/EM &c manual.
|
|
||||||
//
|
|
||||||
|
|
||||||
/* Registers */
|
|
||||||
#define E1000_CTL (0x00000/4) /* Device Control Register - RW */
|
|
||||||
#define E1000_ICR (0x000C0/4) /* Interrupt Cause Read - R */
|
|
||||||
#define E1000_IMS (0x000D0/4) /* Interrupt Mask Set - RW */
|
|
||||||
#define E1000_RCTL (0x00100/4) /* RX Control - RW */
|
|
||||||
#define E1000_TCTL (0x00400/4) /* TX Control - RW */
|
|
||||||
#define E1000_TIPG (0x00410/4) /* TX Inter-packet gap -RW */
|
|
||||||
#define E1000_RDBAL (0x02800/4) /* RX Descriptor Base Address Low - RW */
|
|
||||||
#define E1000_RDTR (0x02820/4) /* RX Delay Timer */
|
|
||||||
#define E1000_RADV (0x0282C/4) /* RX Interrupt Absolute Delay Timer */
|
|
||||||
#define E1000_RDH (0x02810/4) /* RX Descriptor Head - RW */
|
|
||||||
#define E1000_RDT (0x02818/4) /* RX Descriptor Tail - RW */
|
|
||||||
#define E1000_RDLEN (0x02808/4) /* RX Descriptor Length - RW */
|
|
||||||
#define E1000_RSRPD (0x02C00/4) /* RX Small Packet Detect Interrupt */
|
|
||||||
#define E1000_TDBAL (0x03800/4) /* TX Descriptor Base Address Low - RW */
|
|
||||||
#define E1000_TDLEN (0x03808/4) /* TX Descriptor Length - RW */
|
|
||||||
#define E1000_TDH (0x03810/4) /* TX Descriptor Head - RW */
|
|
||||||
#define E1000_TDT (0x03818/4) /* TX Descripotr Tail - RW */
|
|
||||||
#define E1000_MTA (0x05200/4) /* Multicast Table Array - RW Array */
|
|
||||||
#define E1000_RA (0x05400/4) /* Receive Address - RW Array */
|
|
||||||
|
|
||||||
/* Device Control */
|
|
||||||
#define E1000_CTL_SLU 0x00000040 /* set link up */
|
|
||||||
#define E1000_CTL_FRCSPD 0x00000800 /* force speed */
|
|
||||||
#define E1000_CTL_FRCDPLX 0x00001000 /* force duplex */
|
|
||||||
#define E1000_CTL_RST 0x00400000 /* full reset */
|
|
||||||
|
|
||||||
/* Transmit Control */
|
|
||||||
#define E1000_TCTL_RST 0x00000001 /* software reset */
|
|
||||||
#define E1000_TCTL_EN 0x00000002 /* enable tx */
|
|
||||||
#define E1000_TCTL_BCE 0x00000004 /* busy check enable */
|
|
||||||
#define E1000_TCTL_PSP 0x00000008 /* pad short packets */
|
|
||||||
#define E1000_TCTL_CT 0x00000ff0 /* collision threshold */
|
|
||||||
#define E1000_TCTL_CT_SHIFT 4
|
|
||||||
#define E1000_TCTL_COLD 0x003ff000 /* collision distance */
|
|
||||||
#define E1000_TCTL_COLD_SHIFT 12
|
|
||||||
#define E1000_TCTL_SWXOFF 0x00400000 /* SW Xoff transmission */
|
|
||||||
#define E1000_TCTL_PBE 0x00800000 /* Packet Burst Enable */
|
|
||||||
#define E1000_TCTL_RTLC 0x01000000 /* Re-transmit on late collision */
|
|
||||||
#define E1000_TCTL_NRTU 0x02000000 /* No Re-transmit on underrun */
|
|
||||||
#define E1000_TCTL_MULR 0x10000000 /* Multiple request support */
|
|
||||||
|
|
||||||
/* Receive Control */
|
|
||||||
#define E1000_RCTL_RST 0x00000001 /* Software reset */
|
|
||||||
#define E1000_RCTL_EN 0x00000002 /* enable */
|
|
||||||
#define E1000_RCTL_SBP 0x00000004 /* store bad packet */
|
|
||||||
#define E1000_RCTL_UPE 0x00000008 /* unicast promiscuous enable */
|
|
||||||
#define E1000_RCTL_MPE 0x00000010 /* multicast promiscuous enab */
|
|
||||||
#define E1000_RCTL_LPE 0x00000020 /* long packet enable */
|
|
||||||
#define E1000_RCTL_LBM_NO 0x00000000 /* no loopback mode */
|
|
||||||
#define E1000_RCTL_LBM_MAC 0x00000040 /* MAC loopback mode */
|
|
||||||
#define E1000_RCTL_LBM_SLP 0x00000080 /* serial link loopback mode */
|
|
||||||
#define E1000_RCTL_LBM_TCVR 0x000000C0 /* tcvr loopback mode */
|
|
||||||
#define E1000_RCTL_DTYP_MASK 0x00000C00 /* Descriptor type mask */
|
|
||||||
#define E1000_RCTL_DTYP_PS 0x00000400 /* Packet Split descriptor */
|
|
||||||
#define E1000_RCTL_RDMTS_HALF 0x00000000 /* rx desc min threshold size */
|
|
||||||
#define E1000_RCTL_RDMTS_QUAT 0x00000100 /* rx desc min threshold size */
|
|
||||||
#define E1000_RCTL_RDMTS_EIGTH 0x00000200 /* rx desc min threshold size */
|
|
||||||
#define E1000_RCTL_MO_SHIFT 12 /* multicast offset shift */
|
|
||||||
#define E1000_RCTL_MO_0 0x00000000 /* multicast offset 11:0 */
|
|
||||||
#define E1000_RCTL_MO_1 0x00001000 /* multicast offset 12:1 */
|
|
||||||
#define E1000_RCTL_MO_2 0x00002000 /* multicast offset 13:2 */
|
|
||||||
#define E1000_RCTL_MO_3 0x00003000 /* multicast offset 15:4 */
|
|
||||||
#define E1000_RCTL_MDR 0x00004000 /* multicast desc ring 0 */
|
|
||||||
#define E1000_RCTL_BAM 0x00008000 /* broadcast enable */
|
|
||||||
/* these buffer sizes are valid if E1000_RCTL_BSEX is 0 */
|
|
||||||
#define E1000_RCTL_SZ_2048 0x00000000 /* rx buffer size 2048 */
|
|
||||||
#define E1000_RCTL_SZ_1024 0x00010000 /* rx buffer size 1024 */
|
|
||||||
#define E1000_RCTL_SZ_512 0x00020000 /* rx buffer size 512 */
|
|
||||||
#define E1000_RCTL_SZ_256 0x00030000 /* rx buffer size 256 */
|
|
||||||
/* these buffer sizes are valid if E1000_RCTL_BSEX is 1 */
|
|
||||||
#define E1000_RCTL_SZ_16384 0x00010000 /* rx buffer size 16384 */
|
|
||||||
#define E1000_RCTL_SZ_8192 0x00020000 /* rx buffer size 8192 */
|
|
||||||
#define E1000_RCTL_SZ_4096 0x00030000 /* rx buffer size 4096 */
|
|
||||||
#define E1000_RCTL_VFE 0x00040000 /* vlan filter enable */
|
|
||||||
#define E1000_RCTL_CFIEN 0x00080000 /* canonical form enable */
|
|
||||||
#define E1000_RCTL_CFI 0x00100000 /* canonical form indicator */
|
|
||||||
#define E1000_RCTL_DPF 0x00400000 /* discard pause frames */
|
|
||||||
#define E1000_RCTL_PMCF 0x00800000 /* pass MAC control frames */
|
|
||||||
#define E1000_RCTL_BSEX 0x02000000 /* Buffer size extension */
|
|
||||||
#define E1000_RCTL_SECRC 0x04000000 /* Strip Ethernet CRC */
|
|
||||||
#define E1000_RCTL_FLXBUF_MASK 0x78000000 /* Flexible buffer size */
|
|
||||||
#define E1000_RCTL_FLXBUF_SHIFT 27 /* Flexible buffer shift */
|
|
||||||
|
|
||||||
#define DATA_MAX 1518
|
|
||||||
|
|
||||||
/* Transmit Descriptor command definitions [E1000 3.3.3.1] */
|
|
||||||
#define E1000_TXD_CMD_EOP 0x01 /* End of Packet */
|
|
||||||
#define E1000_TXD_CMD_RS 0x08 /* Report Status */
|
|
||||||
|
|
||||||
/* Transmit Descriptor status definitions [E1000 3.3.3.2] */
|
|
||||||
#define E1000_TXD_STAT_DD 0x00000001 /* Descriptor Done */
|
|
||||||
|
|
||||||
// [E1000 3.3.3]
|
|
||||||
struct tx_desc
|
|
||||||
{
|
|
||||||
uint64 addr;
|
|
||||||
uint16 length;
|
|
||||||
uint8 cso;
|
|
||||||
uint8 cmd;
|
|
||||||
uint8 status;
|
|
||||||
uint8 css;
|
|
||||||
uint16 special;
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Receive Descriptor bit definitions [E1000 3.2.3.1] */
|
|
||||||
#define E1000_RXD_STAT_DD 0x01 /* Descriptor Done */
|
|
||||||
#define E1000_RXD_STAT_EOP 0x02 /* End of Packet */
|
|
||||||
|
|
||||||
// [E1000 3.2.3]
|
|
||||||
struct rx_desc
|
|
||||||
{
|
|
||||||
uint64 addr; /* Address of the descriptor's data buffer */
|
|
||||||
uint16 length; /* Length of data DMAed into data buffer */
|
|
||||||
uint16 csum; /* Packet checksum */
|
|
||||||
uint8 status; /* Descriptor status */
|
|
||||||
uint8 errors; /* Descriptor Errors */
|
|
||||||
uint16 special;
|
|
||||||
};
|
|
||||||
|
|
||||||
@ -80,11 +80,6 @@ fileclose(struct file *f)
|
|||||||
iput(ff.ip);
|
iput(ff.ip);
|
||||||
end_op();
|
end_op();
|
||||||
}
|
}
|
||||||
#ifdef LAB_NET
|
|
||||||
else if(ff.type == FD_SOCK){
|
|
||||||
sockclose(ff.sock);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get metadata about file f.
|
// Get metadata about file f.
|
||||||
@ -127,13 +122,7 @@ fileread(struct file *f, uint64 addr, int n)
|
|||||||
if((r = readi(f->ip, 1, addr, f->off, n)) > 0)
|
if((r = readi(f->ip, 1, addr, f->off, n)) > 0)
|
||||||
f->off += r;
|
f->off += r;
|
||||||
iunlock(f->ip);
|
iunlock(f->ip);
|
||||||
}
|
} else {
|
||||||
#ifdef LAB_NET
|
|
||||||
else if(f->type == FD_SOCK){
|
|
||||||
r = sockread(f->sock, addr, n);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else {
|
|
||||||
panic("fileread");
|
panic("fileread");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,13 +173,7 @@ filewrite(struct file *f, uint64 addr, int n)
|
|||||||
i += r;
|
i += r;
|
||||||
}
|
}
|
||||||
ret = (i == n ? n : -1);
|
ret = (i == n ? n : -1);
|
||||||
}
|
} else {
|
||||||
#ifdef LAB_NET
|
|
||||||
else if(f->type == FD_SOCK){
|
|
||||||
ret = sockwrite(f->sock, addr, n);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else {
|
|
||||||
panic("filewrite");
|
panic("filewrite");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,17 +1,10 @@
|
|||||||
struct file {
|
struct file {
|
||||||
#ifdef LAB_NET
|
|
||||||
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE, FD_SOCK } type;
|
|
||||||
#else
|
|
||||||
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type;
|
enum { FD_NONE, FD_PIPE, FD_INODE, FD_DEVICE } type;
|
||||||
#endif
|
|
||||||
int ref; // reference count
|
int ref; // reference count
|
||||||
char readable;
|
char readable;
|
||||||
char writable;
|
char writable;
|
||||||
struct pipe *pipe; // FD_PIPE
|
struct pipe *pipe; // FD_PIPE
|
||||||
struct inode *ip; // FD_INODE and FD_DEVICE
|
struct inode *ip; // FD_INODE and FD_DEVICE
|
||||||
#ifdef LAB_NET
|
|
||||||
struct sock *sock; // FD_SOCK
|
|
||||||
#endif
|
|
||||||
uint off; // FD_INODE
|
uint off; // FD_INODE
|
||||||
short major; // FD_DEVICE
|
short major; // FD_DEVICE
|
||||||
};
|
};
|
||||||
@ -45,4 +38,3 @@ struct devsw {
|
|||||||
extern struct devsw devsw[];
|
extern struct devsw devsw[];
|
||||||
|
|
||||||
#define CONSOLE 1
|
#define CONSOLE 1
|
||||||
#define STATS 2
|
|
||||||
|
|||||||
@ -12,9 +12,6 @@ main()
|
|||||||
{
|
{
|
||||||
if(cpuid() == 0){
|
if(cpuid() == 0){
|
||||||
consoleinit();
|
consoleinit();
|
||||||
#if defined(LAB_LOCK)
|
|
||||||
statsinit();
|
|
||||||
#endif
|
|
||||||
printfinit();
|
printfinit();
|
||||||
printf("\n");
|
printf("\n");
|
||||||
printf("xv6 kernel is booting\n");
|
printf("xv6 kernel is booting\n");
|
||||||
@ -31,18 +28,11 @@ main()
|
|||||||
iinit(); // inode table
|
iinit(); // inode table
|
||||||
fileinit(); // file table
|
fileinit(); // file table
|
||||||
virtio_disk_init(); // emulated hard disk
|
virtio_disk_init(); // emulated hard disk
|
||||||
#ifdef LAB_NET
|
|
||||||
pci_init();
|
|
||||||
sockinit();
|
|
||||||
#endif
|
|
||||||
userinit(); // first user process
|
userinit(); // first user process
|
||||||
#ifdef KCSAN
|
|
||||||
kcsaninit();
|
|
||||||
#endif
|
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
started = 1;
|
started = 1;
|
||||||
} else {
|
} else {
|
||||||
while(lockfree_read4((int *) &started) == 0)
|
while(started == 0)
|
||||||
;
|
;
|
||||||
__sync_synchronize();
|
__sync_synchronize();
|
||||||
printf("hart %d starting\n", cpuid());
|
printf("hart %d starting\n", cpuid());
|
||||||
|
|||||||
@ -25,10 +25,6 @@
|
|||||||
#define VIRTIO0 0x10001000
|
#define VIRTIO0 0x10001000
|
||||||
#define VIRTIO0_IRQ 1
|
#define VIRTIO0_IRQ 1
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
#define E1000_IRQ 33
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// core local interruptor (CLINT), which contains the timer.
|
// core local interruptor (CLINT), which contains the timer.
|
||||||
#define CLINT 0x2000000L
|
#define CLINT 0x2000000L
|
||||||
#define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8*(hartid))
|
#define CLINT_MTIMECMP(hartid) (CLINT + 0x4000 + 8*(hartid))
|
||||||
@ -57,7 +53,7 @@
|
|||||||
|
|
||||||
// map kernel stacks beneath the trampoline,
|
// map kernel stacks beneath the trampoline,
|
||||||
// each surrounded by invalid guard pages.
|
// each surrounded by invalid guard pages.
|
||||||
#define KSTACK(p) (TRAMPOLINE - (p)*2*PGSIZE - 3*PGSIZE)
|
#define KSTACK(p) (TRAMPOLINE - ((p)+1)* 2*PGSIZE)
|
||||||
|
|
||||||
// User memory layout.
|
// User memory layout.
|
||||||
// Address zero first:
|
// Address zero first:
|
||||||
@ -66,14 +62,6 @@
|
|||||||
// fixed-size stack
|
// fixed-size stack
|
||||||
// expandable heap
|
// expandable heap
|
||||||
// ...
|
// ...
|
||||||
// USYSCALL (shared with kernel)
|
|
||||||
// TRAPFRAME (p->trapframe, used by the trampoline)
|
// TRAPFRAME (p->trapframe, used by the trampoline)
|
||||||
// TRAMPOLINE (the same page as in the kernel)
|
// TRAMPOLINE (the same page as in the kernel)
|
||||||
#define TRAPFRAME (TRAMPOLINE - PGSIZE)
|
#define TRAPFRAME (TRAMPOLINE - PGSIZE)
|
||||||
#ifdef LAB_PGTBL
|
|
||||||
#define USYSCALL (TRAPFRAME - PGSIZE)
|
|
||||||
|
|
||||||
struct usyscall {
|
|
||||||
int pid; // Process ID
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|||||||
374
kernel/net.c
374
kernel/net.c
@ -1,374 +0,0 @@
|
|||||||
//
|
|
||||||
// networking protocol support (IP, UDP, ARP, etc.).
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
#include "param.h"
|
|
||||||
#include "memlayout.h"
|
|
||||||
#include "riscv.h"
|
|
||||||
#include "spinlock.h"
|
|
||||||
#include "proc.h"
|
|
||||||
#include "net.h"
|
|
||||||
#include "defs.h"
|
|
||||||
|
|
||||||
static uint32 local_ip = MAKE_IP_ADDR(10, 0, 2, 15); // qemu's idea of the guest IP
|
|
||||||
static uint8 local_mac[ETHADDR_LEN] = { 0x52, 0x54, 0x00, 0x12, 0x34, 0x56 };
|
|
||||||
static uint8 broadcast_mac[ETHADDR_LEN] = { 0xFF, 0XFF, 0XFF, 0XFF, 0XFF, 0XFF };
|
|
||||||
|
|
||||||
// Strips data from the start of the buffer and returns a pointer to it.
|
|
||||||
// Returns 0 if less than the full requested length is available.
|
|
||||||
char *
|
|
||||||
mbufpull(struct mbuf *m, unsigned int len)
|
|
||||||
{
|
|
||||||
char *tmp = m->head;
|
|
||||||
if (m->len < len)
|
|
||||||
return 0;
|
|
||||||
m->len -= len;
|
|
||||||
m->head += len;
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepends data to the beginning of the buffer and returns a pointer to it.
|
|
||||||
char *
|
|
||||||
mbufpush(struct mbuf *m, unsigned int len)
|
|
||||||
{
|
|
||||||
m->head -= len;
|
|
||||||
if (m->head < m->buf)
|
|
||||||
panic("mbufpush");
|
|
||||||
m->len += len;
|
|
||||||
return m->head;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Appends data to the end of the buffer and returns a pointer to it.
|
|
||||||
char *
|
|
||||||
mbufput(struct mbuf *m, unsigned int len)
|
|
||||||
{
|
|
||||||
char *tmp = m->head + m->len;
|
|
||||||
m->len += len;
|
|
||||||
if (m->len > MBUF_SIZE)
|
|
||||||
panic("mbufput");
|
|
||||||
return tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strips data from the end of the buffer and returns a pointer to it.
|
|
||||||
// Returns 0 if less than the full requested length is available.
|
|
||||||
char *
|
|
||||||
mbuftrim(struct mbuf *m, unsigned int len)
|
|
||||||
{
|
|
||||||
if (len > m->len)
|
|
||||||
return 0;
|
|
||||||
m->len -= len;
|
|
||||||
return m->head + m->len;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Allocates a packet buffer.
|
|
||||||
struct mbuf *
|
|
||||||
mbufalloc(unsigned int headroom)
|
|
||||||
{
|
|
||||||
struct mbuf *m;
|
|
||||||
|
|
||||||
if (headroom > MBUF_SIZE)
|
|
||||||
return 0;
|
|
||||||
m = kalloc();
|
|
||||||
if (m == 0)
|
|
||||||
return 0;
|
|
||||||
m->next = 0;
|
|
||||||
m->head = (char *)m->buf + headroom;
|
|
||||||
m->len = 0;
|
|
||||||
memset(m->buf, 0, sizeof(m->buf));
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Frees a packet buffer.
|
|
||||||
void
|
|
||||||
mbuffree(struct mbuf *m)
|
|
||||||
{
|
|
||||||
kfree(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pushes an mbuf to the end of the queue.
|
|
||||||
void
|
|
||||||
mbufq_pushtail(struct mbufq *q, struct mbuf *m)
|
|
||||||
{
|
|
||||||
m->next = 0;
|
|
||||||
if (!q->head){
|
|
||||||
q->head = q->tail = m;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
q->tail->next = m;
|
|
||||||
q->tail = m;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pops an mbuf from the start of the queue.
|
|
||||||
struct mbuf *
|
|
||||||
mbufq_pophead(struct mbufq *q)
|
|
||||||
{
|
|
||||||
struct mbuf *head = q->head;
|
|
||||||
if (!head)
|
|
||||||
return 0;
|
|
||||||
q->head = head->next;
|
|
||||||
return head;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns one (nonzero) if the queue is empty.
|
|
||||||
int
|
|
||||||
mbufq_empty(struct mbufq *q)
|
|
||||||
{
|
|
||||||
return q->head == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Intializes a queue of mbufs.
|
|
||||||
void
|
|
||||||
mbufq_init(struct mbufq *q)
|
|
||||||
{
|
|
||||||
q->head = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This code is lifted from FreeBSD's ping.c, and is copyright by the Regents
|
|
||||||
// of the University of California.
|
|
||||||
static unsigned short
|
|
||||||
in_cksum(const unsigned char *addr, int len)
|
|
||||||
{
|
|
||||||
int nleft = len;
|
|
||||||
const unsigned short *w = (const unsigned short *)addr;
|
|
||||||
unsigned int sum = 0;
|
|
||||||
unsigned short answer = 0;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Our algorithm is simple, using a 32 bit accumulator (sum), we add
|
|
||||||
* sequential 16 bit words to it, and at the end, fold back all the
|
|
||||||
* carry bits from the top 16 bits into the lower 16 bits.
|
|
||||||
*/
|
|
||||||
while (nleft > 1) {
|
|
||||||
sum += *w++;
|
|
||||||
nleft -= 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* mop up an odd byte, if necessary */
|
|
||||||
if (nleft == 1) {
|
|
||||||
*(unsigned char *)(&answer) = *(const unsigned char *)w;
|
|
||||||
sum += answer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* add back carry outs from top 16 bits to low 16 bits */
|
|
||||||
sum = (sum & 0xffff) + (sum >> 16);
|
|
||||||
sum += (sum >> 16);
|
|
||||||
/* guaranteed now that the lower 16 bits of sum are correct */
|
|
||||||
|
|
||||||
answer = ~sum; /* truncate to 16 bits */
|
|
||||||
return answer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// sends an ethernet packet
|
|
||||||
static void
|
|
||||||
net_tx_eth(struct mbuf *m, uint16 ethtype)
|
|
||||||
{
|
|
||||||
struct eth *ethhdr;
|
|
||||||
|
|
||||||
ethhdr = mbufpushhdr(m, *ethhdr);
|
|
||||||
memmove(ethhdr->shost, local_mac, ETHADDR_LEN);
|
|
||||||
// In a real networking stack, dhost would be set to the address discovered
|
|
||||||
// through ARP. Because we don't support enough of the ARP protocol, set it
|
|
||||||
// to broadcast instead.
|
|
||||||
memmove(ethhdr->dhost, broadcast_mac, ETHADDR_LEN);
|
|
||||||
ethhdr->type = htons(ethtype);
|
|
||||||
if (e1000_transmit(m)) {
|
|
||||||
mbuffree(m);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sends an IP packet
|
|
||||||
static void
|
|
||||||
net_tx_ip(struct mbuf *m, uint8 proto, uint32 dip)
|
|
||||||
{
|
|
||||||
struct ip *iphdr;
|
|
||||||
|
|
||||||
// push the IP header
|
|
||||||
iphdr = mbufpushhdr(m, *iphdr);
|
|
||||||
memset(iphdr, 0, sizeof(*iphdr));
|
|
||||||
iphdr->ip_vhl = (4 << 4) | (20 >> 2);
|
|
||||||
iphdr->ip_p = proto;
|
|
||||||
iphdr->ip_src = htonl(local_ip);
|
|
||||||
iphdr->ip_dst = htonl(dip);
|
|
||||||
iphdr->ip_len = htons(m->len);
|
|
||||||
iphdr->ip_ttl = 100;
|
|
||||||
iphdr->ip_sum = in_cksum((unsigned char *)iphdr, sizeof(*iphdr));
|
|
||||||
|
|
||||||
// now on to the ethernet layer
|
|
||||||
net_tx_eth(m, ETHTYPE_IP);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sends a UDP packet
|
|
||||||
void
|
|
||||||
net_tx_udp(struct mbuf *m, uint32 dip,
|
|
||||||
uint16 sport, uint16 dport)
|
|
||||||
{
|
|
||||||
struct udp *udphdr;
|
|
||||||
|
|
||||||
// put the UDP header
|
|
||||||
udphdr = mbufpushhdr(m, *udphdr);
|
|
||||||
udphdr->sport = htons(sport);
|
|
||||||
udphdr->dport = htons(dport);
|
|
||||||
udphdr->ulen = htons(m->len);
|
|
||||||
udphdr->sum = 0; // zero means no checksum is provided
|
|
||||||
|
|
||||||
// now on to the IP layer
|
|
||||||
net_tx_ip(m, IPPROTO_UDP, dip);
|
|
||||||
}
|
|
||||||
|
|
||||||
// sends an ARP packet
|
|
||||||
static int
|
|
||||||
net_tx_arp(uint16 op, uint8 dmac[ETHADDR_LEN], uint32 dip)
|
|
||||||
{
|
|
||||||
struct mbuf *m;
|
|
||||||
struct arp *arphdr;
|
|
||||||
|
|
||||||
m = mbufalloc(MBUF_DEFAULT_HEADROOM);
|
|
||||||
if (!m)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// generic part of ARP header
|
|
||||||
arphdr = mbufputhdr(m, *arphdr);
|
|
||||||
arphdr->hrd = htons(ARP_HRD_ETHER);
|
|
||||||
arphdr->pro = htons(ETHTYPE_IP);
|
|
||||||
arphdr->hln = ETHADDR_LEN;
|
|
||||||
arphdr->pln = sizeof(uint32);
|
|
||||||
arphdr->op = htons(op);
|
|
||||||
|
|
||||||
// ethernet + IP part of ARP header
|
|
||||||
memmove(arphdr->sha, local_mac, ETHADDR_LEN);
|
|
||||||
arphdr->sip = htonl(local_ip);
|
|
||||||
memmove(arphdr->tha, dmac, ETHADDR_LEN);
|
|
||||||
arphdr->tip = htonl(dip);
|
|
||||||
|
|
||||||
// header is ready, send the packet
|
|
||||||
net_tx_eth(m, ETHTYPE_ARP);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// receives an ARP packet
|
|
||||||
static void
|
|
||||||
net_rx_arp(struct mbuf *m)
|
|
||||||
{
|
|
||||||
struct arp *arphdr;
|
|
||||||
uint8 smac[ETHADDR_LEN];
|
|
||||||
uint32 sip, tip;
|
|
||||||
|
|
||||||
arphdr = mbufpullhdr(m, *arphdr);
|
|
||||||
if (!arphdr)
|
|
||||||
goto done;
|
|
||||||
|
|
||||||
// validate the ARP header
|
|
||||||
if (ntohs(arphdr->hrd) != ARP_HRD_ETHER ||
|
|
||||||
ntohs(arphdr->pro) != ETHTYPE_IP ||
|
|
||||||
arphdr->hln != ETHADDR_LEN ||
|
|
||||||
arphdr->pln != sizeof(uint32)) {
|
|
||||||
goto done;
|
|
||||||
}
|
|
||||||
|
|
||||||
// only requests are supported so far
|
|
||||||
// check if our IP was solicited
|
|
||||||
tip = ntohl(arphdr->tip); // target IP address
|
|
||||||
if (ntohs(arphdr->op) != ARP_OP_REQUEST || tip != local_ip)
|
|
||||||
goto done;
|
|
||||||
|
|
||||||
// handle the ARP request
|
|
||||||
memmove(smac, arphdr->sha, ETHADDR_LEN); // sender's ethernet address
|
|
||||||
sip = ntohl(arphdr->sip); // sender's IP address (qemu's slirp)
|
|
||||||
net_tx_arp(ARP_OP_REPLY, smac, sip);
|
|
||||||
|
|
||||||
done:
|
|
||||||
mbuffree(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
// receives a UDP packet
|
|
||||||
static void
|
|
||||||
net_rx_udp(struct mbuf *m, uint16 len, struct ip *iphdr)
|
|
||||||
{
|
|
||||||
struct udp *udphdr;
|
|
||||||
uint32 sip;
|
|
||||||
uint16 sport, dport;
|
|
||||||
|
|
||||||
|
|
||||||
udphdr = mbufpullhdr(m, *udphdr);
|
|
||||||
if (!udphdr)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
// TODO: validate UDP checksum
|
|
||||||
|
|
||||||
// validate lengths reported in headers
|
|
||||||
if (ntohs(udphdr->ulen) != len)
|
|
||||||
goto fail;
|
|
||||||
len -= sizeof(*udphdr);
|
|
||||||
if (len > m->len)
|
|
||||||
goto fail;
|
|
||||||
// minimum packet size could be larger than the payload
|
|
||||||
mbuftrim(m, m->len - len);
|
|
||||||
|
|
||||||
// parse the necessary fields
|
|
||||||
sip = ntohl(iphdr->ip_src);
|
|
||||||
sport = ntohs(udphdr->sport);
|
|
||||||
dport = ntohs(udphdr->dport);
|
|
||||||
sockrecvudp(m, sip, dport, sport);
|
|
||||||
return;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
mbuffree(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
// receives an IP packet
|
|
||||||
static void
|
|
||||||
net_rx_ip(struct mbuf *m)
|
|
||||||
{
|
|
||||||
struct ip *iphdr;
|
|
||||||
uint16 len;
|
|
||||||
|
|
||||||
iphdr = mbufpullhdr(m, *iphdr);
|
|
||||||
if (!iphdr)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
// check IP version and header len
|
|
||||||
if (iphdr->ip_vhl != ((4 << 4) | (20 >> 2)))
|
|
||||||
goto fail;
|
|
||||||
// validate IP checksum
|
|
||||||
if (in_cksum((unsigned char *)iphdr, sizeof(*iphdr)))
|
|
||||||
goto fail;
|
|
||||||
// can't support fragmented IP packets
|
|
||||||
if (htons(iphdr->ip_off) != 0)
|
|
||||||
goto fail;
|
|
||||||
// is the packet addressed to us?
|
|
||||||
if (htonl(iphdr->ip_dst) != local_ip)
|
|
||||||
goto fail;
|
|
||||||
// can only support UDP
|
|
||||||
if (iphdr->ip_p != IPPROTO_UDP)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
len = ntohs(iphdr->ip_len) - sizeof(*iphdr);
|
|
||||||
net_rx_udp(m, len, iphdr);
|
|
||||||
return;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
mbuffree(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
// called by e1000 driver's interrupt handler to deliver a packet to the
|
|
||||||
// networking stack
|
|
||||||
void net_rx(struct mbuf *m)
|
|
||||||
{
|
|
||||||
struct eth *ethhdr;
|
|
||||||
uint16 type;
|
|
||||||
|
|
||||||
ethhdr = mbufpullhdr(m, *ethhdr);
|
|
||||||
if (!ethhdr) {
|
|
||||||
mbuffree(m);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
type = ntohs(ethhdr->type);
|
|
||||||
if (type == ETHTYPE_IP)
|
|
||||||
net_rx_ip(m);
|
|
||||||
else if (type == ETHTYPE_ARP)
|
|
||||||
net_rx_arp(m);
|
|
||||||
else
|
|
||||||
mbuffree(m);
|
|
||||||
}
|
|
||||||
173
kernel/net.h
173
kernel/net.h
@ -1,173 +0,0 @@
|
|||||||
//
|
|
||||||
// packet buffer management
|
|
||||||
//
|
|
||||||
|
|
||||||
#define MBUF_SIZE 2048
|
|
||||||
#define MBUF_DEFAULT_HEADROOM 128
|
|
||||||
|
|
||||||
struct mbuf {
|
|
||||||
struct mbuf *next; // the next mbuf in the chain
|
|
||||||
char *head; // the current start position of the buffer
|
|
||||||
unsigned int len; // the length of the buffer
|
|
||||||
char buf[MBUF_SIZE]; // the backing store
|
|
||||||
};
|
|
||||||
|
|
||||||
char *mbufpull(struct mbuf *m, unsigned int len);
|
|
||||||
char *mbufpush(struct mbuf *m, unsigned int len);
|
|
||||||
char *mbufput(struct mbuf *m, unsigned int len);
|
|
||||||
char *mbuftrim(struct mbuf *m, unsigned int len);
|
|
||||||
|
|
||||||
// The above functions manipulate the size and position of the buffer:
|
|
||||||
// <- push <- trim
|
|
||||||
// -> pull -> put
|
|
||||||
// [-headroom-][------buffer------][-tailroom-]
|
|
||||||
// |----------------MBUF_SIZE-----------------|
|
|
||||||
//
|
|
||||||
// These marcos automatically typecast and determine the size of header structs.
|
|
||||||
// In most situations you should use these instead of the raw ops above.
|
|
||||||
#define mbufpullhdr(mbuf, hdr) (typeof(hdr)*)mbufpull(mbuf, sizeof(hdr))
|
|
||||||
#define mbufpushhdr(mbuf, hdr) (typeof(hdr)*)mbufpush(mbuf, sizeof(hdr))
|
|
||||||
#define mbufputhdr(mbuf, hdr) (typeof(hdr)*)mbufput(mbuf, sizeof(hdr))
|
|
||||||
#define mbuftrimhdr(mbuf, hdr) (typeof(hdr)*)mbuftrim(mbuf, sizeof(hdr))
|
|
||||||
|
|
||||||
struct mbuf *mbufalloc(unsigned int headroom);
|
|
||||||
void mbuffree(struct mbuf *m);
|
|
||||||
|
|
||||||
struct mbufq {
|
|
||||||
struct mbuf *head; // the first element in the queue
|
|
||||||
struct mbuf *tail; // the last element in the queue
|
|
||||||
};
|
|
||||||
|
|
||||||
void mbufq_pushtail(struct mbufq *q, struct mbuf *m);
|
|
||||||
struct mbuf *mbufq_pophead(struct mbufq *q);
|
|
||||||
int mbufq_empty(struct mbufq *q);
|
|
||||||
void mbufq_init(struct mbufq *q);
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// endianness support
|
|
||||||
//
|
|
||||||
|
|
||||||
static inline uint16 bswaps(uint16 val)
|
|
||||||
{
|
|
||||||
return (((val & 0x00ffU) << 8) |
|
|
||||||
((val & 0xff00U) >> 8));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32 bswapl(uint32 val)
|
|
||||||
{
|
|
||||||
return (((val & 0x000000ffUL) << 24) |
|
|
||||||
((val & 0x0000ff00UL) << 8) |
|
|
||||||
((val & 0x00ff0000UL) >> 8) |
|
|
||||||
((val & 0xff000000UL) >> 24));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use these macros to convert network bytes to the native byte order.
|
|
||||||
// Note that Risc-V uses little endian while network order is big endian.
|
|
||||||
#define ntohs bswaps
|
|
||||||
#define ntohl bswapl
|
|
||||||
#define htons bswaps
|
|
||||||
#define htonl bswapl
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// useful networking headers
|
|
||||||
//
|
|
||||||
|
|
||||||
#define ETHADDR_LEN 6
|
|
||||||
|
|
||||||
// an Ethernet packet header (start of the packet).
|
|
||||||
struct eth {
|
|
||||||
uint8 dhost[ETHADDR_LEN];
|
|
||||||
uint8 shost[ETHADDR_LEN];
|
|
||||||
uint16 type;
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
#define ETHTYPE_IP 0x0800 // Internet protocol
|
|
||||||
#define ETHTYPE_ARP 0x0806 // Address resolution protocol
|
|
||||||
|
|
||||||
// an IP packet header (comes after an Ethernet header).
|
|
||||||
struct ip {
|
|
||||||
uint8 ip_vhl; // version << 4 | header length >> 2
|
|
||||||
uint8 ip_tos; // type of service
|
|
||||||
uint16 ip_len; // total length
|
|
||||||
uint16 ip_id; // identification
|
|
||||||
uint16 ip_off; // fragment offset field
|
|
||||||
uint8 ip_ttl; // time to live
|
|
||||||
uint8 ip_p; // protocol
|
|
||||||
uint16 ip_sum; // checksum
|
|
||||||
uint32 ip_src, ip_dst;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define IPPROTO_ICMP 1 // Control message protocol
|
|
||||||
#define IPPROTO_TCP 6 // Transmission control protocol
|
|
||||||
#define IPPROTO_UDP 17 // User datagram protocol
|
|
||||||
|
|
||||||
#define MAKE_IP_ADDR(a, b, c, d) \
|
|
||||||
(((uint32)a << 24) | ((uint32)b << 16) | \
|
|
||||||
((uint32)c << 8) | (uint32)d)
|
|
||||||
|
|
||||||
// a UDP packet header (comes after an IP header).
|
|
||||||
struct udp {
|
|
||||||
uint16 sport; // source port
|
|
||||||
uint16 dport; // destination port
|
|
||||||
uint16 ulen; // length, including udp header, not including IP header
|
|
||||||
uint16 sum; // checksum
|
|
||||||
};
|
|
||||||
|
|
||||||
// an ARP packet (comes after an Ethernet header).
|
|
||||||
struct arp {
|
|
||||||
uint16 hrd; // format of hardware address
|
|
||||||
uint16 pro; // format of protocol address
|
|
||||||
uint8 hln; // length of hardware address
|
|
||||||
uint8 pln; // length of protocol address
|
|
||||||
uint16 op; // operation
|
|
||||||
|
|
||||||
char sha[ETHADDR_LEN]; // sender hardware address
|
|
||||||
uint32 sip; // sender IP address
|
|
||||||
char tha[ETHADDR_LEN]; // target hardware address
|
|
||||||
uint32 tip; // target IP address
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
#define ARP_HRD_ETHER 1 // Ethernet
|
|
||||||
|
|
||||||
enum {
|
|
||||||
ARP_OP_REQUEST = 1, // requests hw addr given protocol addr
|
|
||||||
ARP_OP_REPLY = 2, // replies a hw addr given protocol addr
|
|
||||||
};
|
|
||||||
|
|
||||||
// an DNS packet (comes after an UDP header).
|
|
||||||
struct dns {
|
|
||||||
uint16 id; // request ID
|
|
||||||
|
|
||||||
uint8 rd: 1; // recursion desired
|
|
||||||
uint8 tc: 1; // truncated
|
|
||||||
uint8 aa: 1; // authoritive
|
|
||||||
uint8 opcode: 4;
|
|
||||||
uint8 qr: 1; // query/response
|
|
||||||
uint8 rcode: 4; // response code
|
|
||||||
uint8 cd: 1; // checking disabled
|
|
||||||
uint8 ad: 1; // authenticated data
|
|
||||||
uint8 z: 1;
|
|
||||||
uint8 ra: 1; // recursion available
|
|
||||||
|
|
||||||
uint16 qdcount; // number of question entries
|
|
||||||
uint16 ancount; // number of resource records in answer section
|
|
||||||
uint16 nscount; // number of NS resource records in authority section
|
|
||||||
uint16 arcount; // number of resource records in additional records
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
struct dns_question {
|
|
||||||
uint16 qtype;
|
|
||||||
uint16 qclass;
|
|
||||||
} __attribute__((packed));
|
|
||||||
|
|
||||||
#define ARECORD (0x0001)
|
|
||||||
#define QCLASS (0x0001)
|
|
||||||
|
|
||||||
struct dns_data {
|
|
||||||
uint16 type;
|
|
||||||
uint16 class;
|
|
||||||
uint32 ttl;
|
|
||||||
uint16 len;
|
|
||||||
} __attribute__((packed));
|
|
||||||
61
kernel/pci.c
61
kernel/pci.c
@ -1,61 +0,0 @@
|
|||||||
//
|
|
||||||
// simple PCI-Express initialization, only
|
|
||||||
// works for qemu and its e1000 card.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
#include "param.h"
|
|
||||||
#include "memlayout.h"
|
|
||||||
#include "riscv.h"
|
|
||||||
#include "spinlock.h"
|
|
||||||
#include "proc.h"
|
|
||||||
#include "defs.h"
|
|
||||||
|
|
||||||
void
|
|
||||||
pci_init()
|
|
||||||
{
|
|
||||||
// we'll place the e1000 registers at this address.
|
|
||||||
// vm.c maps this range.
|
|
||||||
uint64 e1000_regs = 0x40000000L;
|
|
||||||
|
|
||||||
// qemu -machine virt puts PCIe config space here.
|
|
||||||
// vm.c maps this range.
|
|
||||||
uint32 *ecam = (uint32 *) 0x30000000L;
|
|
||||||
|
|
||||||
// look at each possible PCI device on bus 0.
|
|
||||||
for(int dev = 0; dev < 32; dev++){
|
|
||||||
int bus = 0;
|
|
||||||
int func = 0;
|
|
||||||
int offset = 0;
|
|
||||||
uint32 off = (bus << 16) | (dev << 11) | (func << 8) | (offset);
|
|
||||||
volatile uint32 *base = ecam + off;
|
|
||||||
uint32 id = base[0];
|
|
||||||
|
|
||||||
// 100e:8086 is an e1000
|
|
||||||
if(id == 0x100e8086){
|
|
||||||
// command and status register.
|
|
||||||
// bit 0 : I/O access enable
|
|
||||||
// bit 1 : memory access enable
|
|
||||||
// bit 2 : enable mastering
|
|
||||||
base[1] = 7;
|
|
||||||
__sync_synchronize();
|
|
||||||
|
|
||||||
for(int i = 0; i < 6; i++){
|
|
||||||
uint32 old = base[4+i];
|
|
||||||
|
|
||||||
// writing all 1's to the BAR causes it to be
|
|
||||||
// replaced with its size.
|
|
||||||
base[4+i] = 0xffffffff;
|
|
||||||
__sync_synchronize();
|
|
||||||
|
|
||||||
base[4+i] = old;
|
|
||||||
}
|
|
||||||
|
|
||||||
// tell the e1000 to reveal its registers at
|
|
||||||
// physical address 0x40000000.
|
|
||||||
base[4+0] = e1000_regs;
|
|
||||||
|
|
||||||
e1000_init((uint32*)e1000_regs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -14,13 +14,6 @@ plicinit(void)
|
|||||||
// set desired IRQ priorities non-zero (otherwise disabled).
|
// set desired IRQ priorities non-zero (otherwise disabled).
|
||||||
*(uint32*)(PLIC + UART0_IRQ*4) = 1;
|
*(uint32*)(PLIC + UART0_IRQ*4) = 1;
|
||||||
*(uint32*)(PLIC + VIRTIO0_IRQ*4) = 1;
|
*(uint32*)(PLIC + VIRTIO0_IRQ*4) = 1;
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
// PCIE IRQs are 32 to 35
|
|
||||||
for(int irq = 1; irq < 0x35; irq++){
|
|
||||||
*(uint32*)(PLIC + irq*4) = 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -32,11 +25,6 @@ plicinithart(void)
|
|||||||
// for the uart and virtio disk.
|
// for the uart and virtio disk.
|
||||||
*(uint32*)PLIC_SENABLE(hart) = (1 << UART0_IRQ) | (1 << VIRTIO0_IRQ);
|
*(uint32*)PLIC_SENABLE(hart) = (1 << UART0_IRQ) | (1 << VIRTIO0_IRQ);
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
// hack to get at next 32 IRQs for e1000
|
|
||||||
*(uint32*)(PLIC_SENABLE(hart)+4) = 0xffffffff;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// set this hart's S-mode priority threshold to 0.
|
// set this hart's S-mode priority threshold to 0.
|
||||||
*(uint32*)PLIC_SPRIORITY(hart) = 0;
|
*(uint32*)PLIC_SPRIORITY(hart) = 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,52 +8,12 @@
|
|||||||
#include "proc.h"
|
#include "proc.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
|
||||||
#ifdef LAB_LOCK
|
|
||||||
#define NLOCK 500
|
|
||||||
|
|
||||||
static struct spinlock *locks[NLOCK];
|
|
||||||
struct spinlock lock_locks;
|
|
||||||
|
|
||||||
void
|
|
||||||
freelock(struct spinlock *lk)
|
|
||||||
{
|
|
||||||
acquire(&lock_locks);
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < NLOCK; i++) {
|
|
||||||
if(locks[i] == lk) {
|
|
||||||
locks[i] = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
release(&lock_locks);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
findslot(struct spinlock *lk) {
|
|
||||||
acquire(&lock_locks);
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < NLOCK; i++) {
|
|
||||||
if(locks[i] == 0) {
|
|
||||||
locks[i] = lk;
|
|
||||||
release(&lock_locks);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
panic("findslot");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void
|
void
|
||||||
initlock(struct spinlock *lk, char *name)
|
initlock(struct spinlock *lk, char *name)
|
||||||
{
|
{
|
||||||
lk->name = name;
|
lk->name = name;
|
||||||
lk->locked = 0;
|
lk->locked = 0;
|
||||||
lk->cpu = 0;
|
lk->cpu = 0;
|
||||||
#ifdef LAB_LOCK
|
|
||||||
lk->nts = 0;
|
|
||||||
lk->n = 0;
|
|
||||||
findslot(lk);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Acquire the lock.
|
// Acquire the lock.
|
||||||
@ -65,21 +25,12 @@ acquire(struct spinlock *lk)
|
|||||||
if(holding(lk))
|
if(holding(lk))
|
||||||
panic("acquire");
|
panic("acquire");
|
||||||
|
|
||||||
#ifdef LAB_LOCK
|
|
||||||
__sync_fetch_and_add(&(lk->n), 1);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// On RISC-V, sync_lock_test_and_set turns into an atomic swap:
|
// On RISC-V, sync_lock_test_and_set turns into an atomic swap:
|
||||||
// a5 = 1
|
// a5 = 1
|
||||||
// s1 = &lk->locked
|
// s1 = &lk->locked
|
||||||
// amoswap.w.aq a5, a5, (s1)
|
// amoswap.w.aq a5, a5, (s1)
|
||||||
while(__sync_lock_test_and_set(&lk->locked, 1) != 0) {
|
while(__sync_lock_test_and_set(&lk->locked, 1) != 0)
|
||||||
#ifdef LAB_LOCK
|
;
|
||||||
__sync_fetch_and_add(&(lk->nts), 1);
|
|
||||||
#else
|
|
||||||
;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tell the C compiler and the processor to not move loads or stores
|
// Tell the C compiler and the processor to not move loads or stores
|
||||||
// past this point, to ensure that the critical section's memory
|
// past this point, to ensure that the critical section's memory
|
||||||
@ -157,69 +108,3 @@ pop_off(void)
|
|||||||
if(c->noff == 0 && c->intena)
|
if(c->noff == 0 && c->intena)
|
||||||
intr_on();
|
intr_on();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read a shared 64-bit value without holding a lock
|
|
||||||
uint64
|
|
||||||
lockfree_read8(uint64 *addr) {
|
|
||||||
uint64 val;
|
|
||||||
__atomic_load(addr, &val, __ATOMIC_SEQ_CST);
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read a shared 32-bit value without holding a lock
|
|
||||||
int
|
|
||||||
lockfree_read4(int *addr) {
|
|
||||||
uint32 val;
|
|
||||||
__atomic_load(addr, &val, __ATOMIC_SEQ_CST);
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef LAB_LOCK
|
|
||||||
int
|
|
||||||
snprint_lock(char *buf, int sz, struct spinlock *lk)
|
|
||||||
{
|
|
||||||
int n = 0;
|
|
||||||
if(lk->n > 0) {
|
|
||||||
n = snprintf(buf, sz, "lock: %s: #test-and-set %d #acquire() %d\n",
|
|
||||||
lk->name, lk->nts, lk->n);
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
statslock(char *buf, int sz) {
|
|
||||||
int n;
|
|
||||||
int tot = 0;
|
|
||||||
|
|
||||||
acquire(&lock_locks);
|
|
||||||
n = snprintf(buf, sz, "--- lock kmem/bcache stats\n");
|
|
||||||
for(int i = 0; i < NLOCK; i++) {
|
|
||||||
if(locks[i] == 0)
|
|
||||||
break;
|
|
||||||
if(strncmp(locks[i]->name, "bcache", strlen("bcache")) == 0 ||
|
|
||||||
strncmp(locks[i]->name, "kmem", strlen("kmem")) == 0) {
|
|
||||||
tot += locks[i]->nts;
|
|
||||||
n += snprint_lock(buf +n, sz-n, locks[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
n += snprintf(buf+n, sz-n, "--- top 5 contended locks:\n");
|
|
||||||
int last = 100000000;
|
|
||||||
// stupid way to compute top 5 contended locks
|
|
||||||
for(int t = 0; t < 5; t++) {
|
|
||||||
int top = 0;
|
|
||||||
for(int i = 0; i < NLOCK; i++) {
|
|
||||||
if(locks[i] == 0)
|
|
||||||
break;
|
|
||||||
if(locks[i]->nts > locks[top]->nts && locks[i]->nts < last) {
|
|
||||||
top = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n += snprint_lock(buf+n, sz-n, locks[top]);
|
|
||||||
last = locks[top]->nts;
|
|
||||||
}
|
|
||||||
n += snprintf(buf+n, sz-n, "tot= %d\n", tot);
|
|
||||||
release(&lock_locks);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|||||||
@ -102,13 +102,6 @@ extern uint64 sys_link(void);
|
|||||||
extern uint64 sys_mkdir(void);
|
extern uint64 sys_mkdir(void);
|
||||||
extern uint64 sys_close(void);
|
extern uint64 sys_close(void);
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
extern uint64 sys_connect(void);
|
|
||||||
#endif
|
|
||||||
#ifdef LAB_PGTBL
|
|
||||||
extern uint64 sys_pgaccess(void);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// An array mapping syscall numbers from syscall.h
|
// An array mapping syscall numbers from syscall.h
|
||||||
// to the function that handles the system call.
|
// to the function that handles the system call.
|
||||||
static uint64 (*syscalls[])(void) = {
|
static uint64 (*syscalls[])(void) = {
|
||||||
@ -133,16 +126,8 @@ static uint64 (*syscalls[])(void) = {
|
|||||||
[SYS_link] sys_link,
|
[SYS_link] sys_link,
|
||||||
[SYS_mkdir] sys_mkdir,
|
[SYS_mkdir] sys_mkdir,
|
||||||
[SYS_close] sys_close,
|
[SYS_close] sys_close,
|
||||||
#ifdef LAB_NET
|
|
||||||
[SYS_connect] sys_connect,
|
|
||||||
#endif
|
|
||||||
#ifdef LAB_PGTBL
|
|
||||||
[SYS_pgaccess] sys_pgaccess,
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
syscall(void)
|
syscall(void)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -20,14 +20,3 @@
|
|||||||
#define SYS_link 19
|
#define SYS_link 19
|
||||||
#define SYS_mkdir 20
|
#define SYS_mkdir 20
|
||||||
#define SYS_close 21
|
#define SYS_close 21
|
||||||
|
|
||||||
// System calls for labs
|
|
||||||
#define SYS_trace 22
|
|
||||||
#define SYS_sysinfo 23
|
|
||||||
#define SYS_sigalarm 24
|
|
||||||
#define SYS_sigreturn 25
|
|
||||||
#define SYS_symlink 26
|
|
||||||
#define SYS_mmap 27
|
|
||||||
#define SYS_munmap 28
|
|
||||||
#define SYS_connect 29
|
|
||||||
#define SYS_pgaccess 30
|
|
||||||
|
|||||||
@ -503,29 +503,3 @@ sys_pipe(void)
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
int
|
|
||||||
sys_connect(void)
|
|
||||||
{
|
|
||||||
struct file *f;
|
|
||||||
int fd;
|
|
||||||
uint32 raddr;
|
|
||||||
uint32 rport;
|
|
||||||
uint32 lport;
|
|
||||||
|
|
||||||
argint(0, (int*)&raddr);
|
|
||||||
argint(1, (int*)&lport);
|
|
||||||
argint(2, (int*)&rport);
|
|
||||||
|
|
||||||
if(sockalloc(&f, raddr, lport, rport) < 0)
|
|
||||||
return -1;
|
|
||||||
if((fd=fdalloc(f)) < 0){
|
|
||||||
fileclose(f);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return fd;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|||||||
185
kernel/sysnet.c
185
kernel/sysnet.c
@ -1,185 +0,0 @@
|
|||||||
//
|
|
||||||
// network system calls.
|
|
||||||
//
|
|
||||||
|
|
||||||
#include "types.h"
|
|
||||||
#include "param.h"
|
|
||||||
#include "memlayout.h"
|
|
||||||
#include "riscv.h"
|
|
||||||
#include "spinlock.h"
|
|
||||||
#include "proc.h"
|
|
||||||
#include "defs.h"
|
|
||||||
#include "fs.h"
|
|
||||||
#include "sleeplock.h"
|
|
||||||
#include "file.h"
|
|
||||||
#include "net.h"
|
|
||||||
|
|
||||||
struct sock {
|
|
||||||
struct sock *next; // the next socket in the list
|
|
||||||
uint32 raddr; // the remote IPv4 address
|
|
||||||
uint16 lport; // the local UDP port number
|
|
||||||
uint16 rport; // the remote UDP port number
|
|
||||||
struct spinlock lock; // protects the rxq
|
|
||||||
struct mbufq rxq; // a queue of packets waiting to be received
|
|
||||||
};
|
|
||||||
|
|
||||||
static struct spinlock lock;
|
|
||||||
static struct sock *sockets;
|
|
||||||
|
|
||||||
void
|
|
||||||
sockinit(void)
|
|
||||||
{
|
|
||||||
initlock(&lock, "socktbl");
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
sockalloc(struct file **f, uint32 raddr, uint16 lport, uint16 rport)
|
|
||||||
{
|
|
||||||
struct sock *si, *pos;
|
|
||||||
|
|
||||||
si = 0;
|
|
||||||
*f = 0;
|
|
||||||
if ((*f = filealloc()) == 0)
|
|
||||||
goto bad;
|
|
||||||
if ((si = (struct sock*)kalloc()) == 0)
|
|
||||||
goto bad;
|
|
||||||
|
|
||||||
// initialize objects
|
|
||||||
si->raddr = raddr;
|
|
||||||
si->lport = lport;
|
|
||||||
si->rport = rport;
|
|
||||||
initlock(&si->lock, "sock");
|
|
||||||
mbufq_init(&si->rxq);
|
|
||||||
(*f)->type = FD_SOCK;
|
|
||||||
(*f)->readable = 1;
|
|
||||||
(*f)->writable = 1;
|
|
||||||
(*f)->sock = si;
|
|
||||||
|
|
||||||
// add to list of sockets
|
|
||||||
acquire(&lock);
|
|
||||||
pos = sockets;
|
|
||||||
while (pos) {
|
|
||||||
if (pos->raddr == raddr &&
|
|
||||||
pos->lport == lport &&
|
|
||||||
pos->rport == rport) {
|
|
||||||
release(&lock);
|
|
||||||
goto bad;
|
|
||||||
}
|
|
||||||
pos = pos->next;
|
|
||||||
}
|
|
||||||
si->next = sockets;
|
|
||||||
sockets = si;
|
|
||||||
release(&lock);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
bad:
|
|
||||||
if (si)
|
|
||||||
kfree((char*)si);
|
|
||||||
if (*f)
|
|
||||||
fileclose(*f);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
sockclose(struct sock *si)
|
|
||||||
{
|
|
||||||
struct sock **pos;
|
|
||||||
struct mbuf *m;
|
|
||||||
|
|
||||||
// remove from list of sockets
|
|
||||||
acquire(&lock);
|
|
||||||
pos = &sockets;
|
|
||||||
while (*pos) {
|
|
||||||
if (*pos == si){
|
|
||||||
*pos = si->next;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
pos = &(*pos)->next;
|
|
||||||
}
|
|
||||||
release(&lock);
|
|
||||||
|
|
||||||
// free any pending mbufs
|
|
||||||
while (!mbufq_empty(&si->rxq)) {
|
|
||||||
m = mbufq_pophead(&si->rxq);
|
|
||||||
mbuffree(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
kfree((char*)si);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
sockread(struct sock *si, uint64 addr, int n)
|
|
||||||
{
|
|
||||||
struct proc *pr = myproc();
|
|
||||||
struct mbuf *m;
|
|
||||||
int len;
|
|
||||||
|
|
||||||
acquire(&si->lock);
|
|
||||||
while (mbufq_empty(&si->rxq) && !pr->killed) {
|
|
||||||
sleep(&si->rxq, &si->lock);
|
|
||||||
}
|
|
||||||
if (pr->killed) {
|
|
||||||
release(&si->lock);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
m = mbufq_pophead(&si->rxq);
|
|
||||||
release(&si->lock);
|
|
||||||
|
|
||||||
len = m->len;
|
|
||||||
if (len > n)
|
|
||||||
len = n;
|
|
||||||
if (copyout(pr->pagetable, addr, m->head, len) == -1) {
|
|
||||||
mbuffree(m);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
mbuffree(m);
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
sockwrite(struct sock *si, uint64 addr, int n)
|
|
||||||
{
|
|
||||||
struct proc *pr = myproc();
|
|
||||||
struct mbuf *m;
|
|
||||||
|
|
||||||
m = mbufalloc(MBUF_DEFAULT_HEADROOM);
|
|
||||||
if (!m)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
if (copyin(pr->pagetable, mbufput(m, n), addr, n) == -1) {
|
|
||||||
mbuffree(m);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
net_tx_udp(m, si->raddr, si->lport, si->rport);
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
// called by protocol handler layer to deliver UDP packets
|
|
||||||
void
|
|
||||||
sockrecvudp(struct mbuf *m, uint32 raddr, uint16 lport, uint16 rport)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// Find the socket that handles this mbuf and deliver it, waking
|
|
||||||
// any sleeping reader. Free the mbuf if there are no sockets
|
|
||||||
// registered to handle it.
|
|
||||||
//
|
|
||||||
struct sock *si;
|
|
||||||
|
|
||||||
acquire(&lock);
|
|
||||||
si = sockets;
|
|
||||||
while (si) {
|
|
||||||
if (si->raddr == raddr && si->lport == lport && si->rport == rport)
|
|
||||||
goto found;
|
|
||||||
si = si->next;
|
|
||||||
}
|
|
||||||
release(&lock);
|
|
||||||
mbuffree(m);
|
|
||||||
return;
|
|
||||||
|
|
||||||
found:
|
|
||||||
acquire(&si->lock);
|
|
||||||
mbufq_pushtail(&si->rxq, m);
|
|
||||||
wakeup(&si->rxq);
|
|
||||||
release(&si->lock);
|
|
||||||
release(&lock);
|
|
||||||
}
|
|
||||||
@ -55,8 +55,6 @@ sys_sleep(void)
|
|||||||
uint ticks0;
|
uint ticks0;
|
||||||
|
|
||||||
argint(0, &n);
|
argint(0, &n);
|
||||||
if(n < 0)
|
|
||||||
n = 0;
|
|
||||||
acquire(&tickslock);
|
acquire(&tickslock);
|
||||||
ticks0 = ticks;
|
ticks0 = ticks;
|
||||||
while(ticks - ticks0 < n){
|
while(ticks - ticks0 < n){
|
||||||
|
|||||||
@ -68,8 +68,6 @@ usertrap(void)
|
|||||||
} else if((which_dev = devintr()) != 0){
|
} else if((which_dev = devintr()) != 0){
|
||||||
// ok
|
// ok
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
|
||||||
printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
|
printf("usertrap(): unexpected scause %p pid=%d\n", r_scause(), p->pid);
|
||||||
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
|
printf(" sepc=%p stval=%p\n", r_sepc(), r_stval());
|
||||||
setkilled(p);
|
setkilled(p);
|
||||||
@ -77,7 +75,6 @@ usertrap(void)
|
|||||||
|
|
||||||
if(killed(p))
|
if(killed(p))
|
||||||
exit(-1);
|
exit(-1);
|
||||||
|
|
||||||
|
|
||||||
// give up the CPU if this is a timer interrupt.
|
// give up the CPU if this is a timer interrupt.
|
||||||
if(which_dev == 2)
|
if(which_dev == 2)
|
||||||
@ -193,13 +190,7 @@ devintr()
|
|||||||
uartintr();
|
uartintr();
|
||||||
} else if(irq == VIRTIO0_IRQ){
|
} else if(irq == VIRTIO0_IRQ){
|
||||||
virtio_disk_intr();
|
virtio_disk_intr();
|
||||||
}
|
} else if(irq){
|
||||||
#ifdef LAB_NET
|
|
||||||
else if(irq == E1000_IRQ){
|
|
||||||
e1000_intr();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
else if(irq){
|
|
||||||
printf("unexpected interrupt irq=%d\n", irq);
|
printf("unexpected interrupt irq=%d\n", irq);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
kernel/vm.c
19
kernel/vm.c
@ -4,8 +4,6 @@
|
|||||||
#include "elf.h"
|
#include "elf.h"
|
||||||
#include "riscv.h"
|
#include "riscv.h"
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
#include "spinlock.h"
|
|
||||||
#include "proc.h"
|
|
||||||
#include "fs.h"
|
#include "fs.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -32,14 +30,6 @@ kvmmake(void)
|
|||||||
// virtio mmio disk interface
|
// virtio mmio disk interface
|
||||||
kvmmap(kpgtbl, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);
|
kvmmap(kpgtbl, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);
|
||||||
|
|
||||||
#ifdef LAB_NET
|
|
||||||
// PCI-E ECAM (configuration space), for pci.c
|
|
||||||
kvmmap(kpgtbl, 0x30000000L, 0x30000000L, 0x10000000, PTE_R | PTE_W);
|
|
||||||
|
|
||||||
// pci.c maps the e1000's registers here.
|
|
||||||
kvmmap(kpgtbl, 0x40000000L, 0x40000000L, 0x20000, PTE_R | PTE_W);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// PLIC
|
// PLIC
|
||||||
kvmmap(kpgtbl, PLIC, PLIC, 0x400000, PTE_R | PTE_W);
|
kvmmap(kpgtbl, PLIC, PLIC, 0x400000, PTE_R | PTE_W);
|
||||||
|
|
||||||
@ -189,10 +179,8 @@ uvmunmap(pagetable_t pagetable, uint64 va, uint64 npages, int do_free)
|
|||||||
for(a = va; a < va + npages*PGSIZE; a += PGSIZE){
|
for(a = va; a < va + npages*PGSIZE; a += PGSIZE){
|
||||||
if((pte = walk(pagetable, a, 0)) == 0)
|
if((pte = walk(pagetable, a, 0)) == 0)
|
||||||
panic("uvmunmap: walk");
|
panic("uvmunmap: walk");
|
||||||
if((*pte & PTE_V) == 0) {
|
if((*pte & PTE_V) == 0)
|
||||||
printf("va=%p pte=%p\n", a, *pte);
|
|
||||||
panic("uvmunmap: not mapped");
|
panic("uvmunmap: not mapped");
|
||||||
}
|
|
||||||
if(PTE_FLAGS(*pte) == PTE_V)
|
if(PTE_FLAGS(*pte) == PTE_V)
|
||||||
panic("uvmunmap: not a leaf");
|
panic("uvmunmap: not a leaf");
|
||||||
if(do_free){
|
if(do_free){
|
||||||
@ -389,7 +377,7 @@ int
|
|||||||
copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)
|
copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)
|
||||||
{
|
{
|
||||||
uint64 n, va0, pa0;
|
uint64 n, va0, pa0;
|
||||||
|
|
||||||
while(len > 0){
|
while(len > 0){
|
||||||
va0 = PGROUNDDOWN(srcva);
|
va0 = PGROUNDDOWN(srcva);
|
||||||
pa0 = walkaddr(pagetable, va0);
|
pa0 = walkaddr(pagetable, va0);
|
||||||
@ -449,6 +437,3 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
BIN
packets.pcap
BIN
packets.pcap
Binary file not shown.
12
ping.py
12
ping.py
@ -1,12 +0,0 @@
|
|||||||
import socket
|
|
||||||
import sys
|
|
||||||
import time
|
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
addr = ('localhost', int(sys.argv[1]))
|
|
||||||
buf = "this is a ping!".encode('utf-8')
|
|
||||||
|
|
||||||
while True:
|
|
||||||
print("pinging...", file=sys.stderr)
|
|
||||||
sock.sendto(buf, ("127.0.0.1", int(sys.argv[1])))
|
|
||||||
time.sleep(1)
|
|
||||||
13
server.py
13
server.py
@ -1,13 +0,0 @@
|
|||||||
import socket
|
|
||||||
import sys
|
|
||||||
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
addr = ('localhost', int(sys.argv[1]))
|
|
||||||
print('listening on %s port %s' % addr, file=sys.stderr)
|
|
||||||
sock.bind(addr)
|
|
||||||
|
|
||||||
while True:
|
|
||||||
buf, raddr = sock.recvfrom(4096)
|
|
||||||
print(buf.decode("utf-8"), file=sys.stderr)
|
|
||||||
if buf:
|
|
||||||
sent = sock.sendto(b'this is the host!', raddr)
|
|
||||||
@ -1,6 +1,5 @@
|
|||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
char buf[512];
|
char buf[512];
|
||||||
@ -33,7 +32,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(i = 1; i < argc; i++){
|
for(i = 1; i < argc; i++){
|
||||||
if((fd = open(argv[i], O_RDONLY)) < 0){
|
if((fd = open(argv[i], 0)) < 0){
|
||||||
fprintf(2, "cat: cannot open %s\n", argv[i]);
|
fprintf(2, "cat: cannot open %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
74
user/find.c
Normal file
74
user/find.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include "kernel/types.h"
|
||||||
|
#include "user/user.h"
|
||||||
|
#include "kernel/stat.h"
|
||||||
|
#include "kernel/fs.h"
|
||||||
|
|
||||||
|
int fmtname(const char *path) {
|
||||||
|
int p;
|
||||||
|
for(p=strlen(path); p >= 0 && *(path + p) != '/'; p--);
|
||||||
|
p++;
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
void find(const char *path, const char* target) {
|
||||||
|
char buf[512], *p;
|
||||||
|
int fd;
|
||||||
|
struct dirent de;
|
||||||
|
struct stat st;
|
||||||
|
if((fd = open(path, 0)) < 0){
|
||||||
|
fprintf(2, "find: cannot open %s\n", path);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(fstat(fd, &st) < 0){
|
||||||
|
fprintf(2, "find: cannot stat %s\n", path);
|
||||||
|
close(fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch(st.type){
|
||||||
|
case T_DEVICE:
|
||||||
|
case T_FILE:
|
||||||
|
if (strcmp(path + fmtname(path), target) == 0) {
|
||||||
|
printf("%s\n", path);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case T_DIR:
|
||||||
|
if (strcmp(path + fmtname(path), target) == 0) {
|
||||||
|
printf("%s\n", path);
|
||||||
|
}
|
||||||
|
if(strlen(path) + 1 + DIRSIZ + 1 > sizeof buf){
|
||||||
|
printf("find: path too long\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
strcpy(buf, path);
|
||||||
|
p = buf+strlen(buf);
|
||||||
|
*p++ = '/';
|
||||||
|
while(read(fd, &de, sizeof(de)) == sizeof(de)){
|
||||||
|
if(de.inum == 0)
|
||||||
|
continue;
|
||||||
|
if (strcmp(de.name, ".") == 0 || strcmp(de.name, "..") == 0)
|
||||||
|
continue;
|
||||||
|
memmove(p, de.name, DIRSIZ);
|
||||||
|
p[DIRSIZ] = 0;
|
||||||
|
if(stat(buf, &st) < 0){
|
||||||
|
printf("find: cannot stat %s\n", buf);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
find(buf, target);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc != 3) {
|
||||||
|
printf("\x1b[31mInvalid arguments\x1b[0m\n");
|
||||||
|
printf("\x1b[32musage\x1b[0m: find <base directory> <file name to find>\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
find(argv[1], argv[2]);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
@ -52,7 +51,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(i = 2; i < argc; i++){
|
for(i = 2; i < argc; i++){
|
||||||
if((fd = open(argv[i], O_RDONLY)) < 0){
|
if((fd = open(argv[i], 0)) < 0){
|
||||||
printf("grep: cannot open %s\n", argv[i]);
|
printf("grep: cannot open %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@
|
|||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
#include "kernel/fs.h"
|
#include "kernel/fs.h"
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
|
|
||||||
char*
|
char*
|
||||||
fmtname(char *path)
|
fmtname(char *path)
|
||||||
@ -31,7 +30,7 @@ ls(char *path)
|
|||||||
struct dirent de;
|
struct dirent de;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
if((fd = open(path, O_RDONLY)) < 0){
|
if((fd = open(path, 0)) < 0){
|
||||||
fprintf(2, "ls: cannot open %s\n", path);
|
fprintf(2, "ls: cannot open %s\n", path);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
297
user/nettests.c
297
user/nettests.c
@ -1,297 +0,0 @@
|
|||||||
#include "kernel/types.h"
|
|
||||||
#include "kernel/net.h"
|
|
||||||
#include "kernel/stat.h"
|
|
||||||
#include "user/user.h"
|
|
||||||
|
|
||||||
//
|
|
||||||
// send a UDP packet to the localhost (outside of qemu),
|
|
||||||
// and receive a response.
|
|
||||||
//
|
|
||||||
static void
|
|
||||||
ping(uint16 sport, uint16 dport, int attempts)
|
|
||||||
{
|
|
||||||
int fd;
|
|
||||||
char *obuf = "a message from xv6!";
|
|
||||||
uint32 dst;
|
|
||||||
|
|
||||||
// 10.0.2.2, which qemu remaps to the external host,
|
|
||||||
// i.e. the machine you're running qemu on.
|
|
||||||
dst = (10 << 24) | (0 << 16) | (2 << 8) | (2 << 0);
|
|
||||||
|
|
||||||
// you can send a UDP packet to any Internet address
|
|
||||||
// by using a different dst.
|
|
||||||
|
|
||||||
if((fd = connect(dst, sport, dport)) < 0){
|
|
||||||
fprintf(2, "ping: connect() failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = 0; i < attempts; i++) {
|
|
||||||
if(write(fd, obuf, strlen(obuf)) < 0){
|
|
||||||
fprintf(2, "ping: send() failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char ibuf[128];
|
|
||||||
int cc = read(fd, ibuf, sizeof(ibuf)-1);
|
|
||||||
if(cc < 0){
|
|
||||||
fprintf(2, "ping: recv() failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
ibuf[cc] = '\0';
|
|
||||||
if(strcmp(ibuf, "this is the host!") != 0){
|
|
||||||
fprintf(2, "ping didn't receive correct payload\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode a DNS name
|
|
||||||
static void
|
|
||||||
encode_qname(char *qn, char *host)
|
|
||||||
{
|
|
||||||
char *l = host;
|
|
||||||
|
|
||||||
for(char *c = host; c < host+strlen(host)+1; c++) {
|
|
||||||
if(*c == '.') {
|
|
||||||
*qn++ = (char) (c-l);
|
|
||||||
for(char *d = l; d < c; d++) {
|
|
||||||
*qn++ = *d;
|
|
||||||
}
|
|
||||||
l = c+1; // skip .
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*qn = '\0';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode a DNS name
|
|
||||||
static void
|
|
||||||
decode_qname(char *qn, int max)
|
|
||||||
{
|
|
||||||
char *qnMax = qn + max;
|
|
||||||
while(1){
|
|
||||||
if(qn >= qnMax){
|
|
||||||
printf("invalid DNS reply\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
int l = *qn;
|
|
||||||
if(l == 0)
|
|
||||||
break;
|
|
||||||
for(int i = 0; i < l; i++) {
|
|
||||||
*qn = *(qn+1);
|
|
||||||
qn++;
|
|
||||||
}
|
|
||||||
*qn++ = '.';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make a DNS request
|
|
||||||
static int
|
|
||||||
dns_req(uint8 *obuf)
|
|
||||||
{
|
|
||||||
int len = 0;
|
|
||||||
|
|
||||||
struct dns *hdr = (struct dns *) obuf;
|
|
||||||
hdr->id = htons(6828);
|
|
||||||
hdr->rd = 1;
|
|
||||||
hdr->qdcount = htons(1);
|
|
||||||
|
|
||||||
len += sizeof(struct dns);
|
|
||||||
|
|
||||||
// qname part of question
|
|
||||||
char *qname = (char *) (obuf + sizeof(struct dns));
|
|
||||||
char *s = "pdos.csail.mit.edu.";
|
|
||||||
encode_qname(qname, s);
|
|
||||||
len += strlen(qname) + 1;
|
|
||||||
|
|
||||||
// constants part of question
|
|
||||||
struct dns_question *h = (struct dns_question *) (qname+strlen(qname)+1);
|
|
||||||
h->qtype = htons(0x1);
|
|
||||||
h->qclass = htons(0x1);
|
|
||||||
|
|
||||||
len += sizeof(struct dns_question);
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process DNS response
|
|
||||||
static void
|
|
||||||
dns_rep(uint8 *ibuf, int cc)
|
|
||||||
{
|
|
||||||
struct dns *hdr = (struct dns *) ibuf;
|
|
||||||
int len;
|
|
||||||
char *qname = 0;
|
|
||||||
int record = 0;
|
|
||||||
|
|
||||||
if(cc < sizeof(struct dns)){
|
|
||||||
printf("DNS reply too short\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!hdr->qr) {
|
|
||||||
printf("Not a DNS reply for %d\n", ntohs(hdr->id));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(hdr->id != htons(6828)){
|
|
||||||
printf("DNS wrong id: %d\n", ntohs(hdr->id));
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(hdr->rcode != 0) {
|
|
||||||
printf("DNS rcode error: %x\n", hdr->rcode);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
//printf("qdcount: %x\n", ntohs(hdr->qdcount));
|
|
||||||
//printf("ancount: %x\n", ntohs(hdr->ancount));
|
|
||||||
//printf("nscount: %x\n", ntohs(hdr->nscount));
|
|
||||||
//printf("arcount: %x\n", ntohs(hdr->arcount));
|
|
||||||
|
|
||||||
len = sizeof(struct dns);
|
|
||||||
|
|
||||||
for(int i =0; i < ntohs(hdr->qdcount); i++) {
|
|
||||||
char *qn = (char *) (ibuf+len);
|
|
||||||
qname = qn;
|
|
||||||
decode_qname(qn, cc - len);
|
|
||||||
len += strlen(qn)+1;
|
|
||||||
len += sizeof(struct dns_question);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i = 0; i < ntohs(hdr->ancount); i++) {
|
|
||||||
if(len >= cc){
|
|
||||||
printf("invalid DNS reply\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *qn = (char *) (ibuf+len);
|
|
||||||
|
|
||||||
if((int) qn[0] > 63) { // compression?
|
|
||||||
qn = (char *)(ibuf+qn[1]);
|
|
||||||
len += 2;
|
|
||||||
} else {
|
|
||||||
decode_qname(qn, cc - len);
|
|
||||||
len += strlen(qn)+1;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct dns_data *d = (struct dns_data *) (ibuf+len);
|
|
||||||
len += sizeof(struct dns_data);
|
|
||||||
//printf("type %d ttl %d len %d\n", ntohs(d->type), ntohl(d->ttl), ntohs(d->len));
|
|
||||||
if(ntohs(d->type) == ARECORD && ntohs(d->len) == 4) {
|
|
||||||
record = 1;
|
|
||||||
printf("DNS arecord for %s is ", qname ? qname : "" );
|
|
||||||
uint8 *ip = (ibuf+len);
|
|
||||||
printf("%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
|
|
||||||
if(ip[0] != 128 || ip[1] != 52 || ip[2] != 129 || ip[3] != 126) {
|
|
||||||
printf("wrong ip address");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
len += 4;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// needed for DNS servers with EDNS support
|
|
||||||
for(int i = 0; i < ntohs(hdr->arcount); i++) {
|
|
||||||
char *qn = (char *) (ibuf+len);
|
|
||||||
if(*qn != 0) {
|
|
||||||
printf("invalid name for EDNS\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
len += 1;
|
|
||||||
|
|
||||||
struct dns_data *d = (struct dns_data *) (ibuf+len);
|
|
||||||
len += sizeof(struct dns_data);
|
|
||||||
if(ntohs(d->type) != 41) {
|
|
||||||
printf("invalid type for EDNS\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
len += ntohs(d->len);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(len != cc) {
|
|
||||||
printf("Processed %d data bytes but received %d\n", len, cc);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if(!record) {
|
|
||||||
printf("Didn't receive an arecord\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
dns()
|
|
||||||
{
|
|
||||||
#define N 1000
|
|
||||||
uint8 obuf[N];
|
|
||||||
uint8 ibuf[N];
|
|
||||||
uint32 dst;
|
|
||||||
int fd;
|
|
||||||
int len;
|
|
||||||
|
|
||||||
memset(obuf, 0, N);
|
|
||||||
memset(ibuf, 0, N);
|
|
||||||
|
|
||||||
// 8.8.8.8: google's name server
|
|
||||||
dst = (8 << 24) | (8 << 16) | (8 << 8) | (8 << 0);
|
|
||||||
|
|
||||||
if((fd = connect(dst, 10000, 53)) < 0){
|
|
||||||
fprintf(2, "ping: connect() failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
len = dns_req(obuf);
|
|
||||||
|
|
||||||
if(write(fd, obuf, len) < 0){
|
|
||||||
fprintf(2, "dns: send() failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
int cc = read(fd, ibuf, sizeof(ibuf));
|
|
||||||
if(cc < 0){
|
|
||||||
fprintf(2, "dns: recv() failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
dns_rep(ibuf, cc);
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int i, ret;
|
|
||||||
uint16 dport = NET_TESTS_PORT;
|
|
||||||
|
|
||||||
printf("nettests running on port %d\n", dport);
|
|
||||||
|
|
||||||
printf("testing ping: ");
|
|
||||||
ping(2000, dport, 1);
|
|
||||||
printf("OK\n");
|
|
||||||
|
|
||||||
printf("testing single-process pings: ");
|
|
||||||
for (i = 0; i < 100; i++)
|
|
||||||
ping(2000, dport, 1);
|
|
||||||
printf("OK\n");
|
|
||||||
|
|
||||||
printf("testing multi-process pings: ");
|
|
||||||
for (i = 0; i < 10; i++){
|
|
||||||
int pid = fork();
|
|
||||||
if (pid == 0){
|
|
||||||
ping(2000 + i + 1, dport, 1);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (i = 0; i < 10; i++){
|
|
||||||
wait(&ret);
|
|
||||||
if (ret != 0)
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
printf("OK\n");
|
|
||||||
|
|
||||||
printf("testing DNS\n");
|
|
||||||
dns();
|
|
||||||
printf("DNS OK\n");
|
|
||||||
|
|
||||||
printf("all tests passed.\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
@ -1,52 +1,27 @@
|
|||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
#define N 5
|
int main(int argc, char** argv) {
|
||||||
char buf[N];
|
int p[2]; // hell, read from p[0] and write to p[1] no matter in pa/ch
|
||||||
|
if (pipe(p) == -1) exit(1);
|
||||||
void
|
uint8 senddata[] = {0xAC};
|
||||||
pong(int *parent_to_child, int *child_to_parent) {
|
uint8 recvdata[] = {0};
|
||||||
if (read(parent_to_child[0], buf, N) < 0) {
|
int pid = fork();
|
||||||
printf("read failed\n");
|
if (pid == 0) {
|
||||||
}
|
// child
|
||||||
printf("%d: received %s\n", getpid(), buf);
|
if (read(p[0], recvdata, 1) != 1) printf("error child read\n");
|
||||||
if (write(child_to_parent[1], "pong", 4) != 4) {
|
close(p[0]);
|
||||||
printf("write failed\n");
|
printf("%d: received ping\n", getpid());
|
||||||
}
|
if (write(p[1], senddata, 1) != 1) printf("error child write\n");
|
||||||
}
|
close(p[1]);
|
||||||
|
exit(0);
|
||||||
void
|
}
|
||||||
ping(int *parent_to_child, int *child_to_parent) {
|
else {
|
||||||
|
if (write(p[1], senddata, 1) != 1) printf("error parent write\n");
|
||||||
if (write(parent_to_child[1], "ping", 4) != 4) {
|
close(p[1]);
|
||||||
printf("write failed\n");
|
if (read(p[0], recvdata, 1) != 1) printf("error parent read\n");
|
||||||
}
|
printf("%d: received pong\n", getpid());
|
||||||
if (read(child_to_parent[0], buf, N) < 0) {
|
close(p[0]);
|
||||||
printf("read failed\n");
|
exit(0);
|
||||||
}
|
}
|
||||||
printf("%d: received %s\n", getpid(), buf);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
int parent_to_child[2];
|
|
||||||
int child_to_parent[2];
|
|
||||||
|
|
||||||
int pid;
|
|
||||||
|
|
||||||
if (pipe(parent_to_child) < 0 || pipe(child_to_parent) < 0) {
|
|
||||||
printf("pipe failed\n");
|
|
||||||
}
|
|
||||||
if ((pid = fork()) < 0) {
|
|
||||||
printf("fork failed\n");
|
|
||||||
}
|
|
||||||
if (pid == 0) {
|
|
||||||
pong(parent_to_child, child_to_parent);
|
|
||||||
} else {
|
|
||||||
ping(parent_to_child, child_to_parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
51
user/primes.c
Normal file
51
user/primes.c
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#include "kernel/types.h"
|
||||||
|
#include "user/user.h"
|
||||||
|
|
||||||
|
void pipeline(int leftfd) {
|
||||||
|
int pipefd[2];
|
||||||
|
int p; read(leftfd, &p, sizeof(p));
|
||||||
|
printf("prime %d\n", p);
|
||||||
|
int n;
|
||||||
|
int has_right = 0;
|
||||||
|
while (read(leftfd, &n, sizeof(n)) > 0) {
|
||||||
|
if (n % p != 0) {
|
||||||
|
if (!has_right) {
|
||||||
|
has_right = 1;
|
||||||
|
pipe(pipefd);
|
||||||
|
if (fork() == 0) {
|
||||||
|
close(pipefd[1]);
|
||||||
|
pipeline(pipefd[0]);
|
||||||
|
} else {
|
||||||
|
close(pipefd[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write(pipefd[1], &n, sizeof(n));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
close(leftfd);
|
||||||
|
if (has_right) {
|
||||||
|
close(pipefd[1]);
|
||||||
|
while(wait(0) != -1);
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
int pipefd[2];
|
||||||
|
pipe(pipefd);
|
||||||
|
int pid = fork();
|
||||||
|
if (pid != 0) {
|
||||||
|
close(pipefd[0]); // no need to read in the feeding proc
|
||||||
|
for (int i = 2; i <= 35; ++ i) {
|
||||||
|
write(pipefd[1], &i, sizeof(i));
|
||||||
|
}
|
||||||
|
close(pipefd[1]);
|
||||||
|
while(wait(0) != -1);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
close(pipefd[1]); // pipeline proc will have its own write pipe
|
||||||
|
pipeline(pipefd[0]);
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
17
user/sleep.c
Normal file
17
user/sleep.c
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "kernel/types.h"
|
||||||
|
#include "user/user.h"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("\x1b[31mUnexpected argument\x1b[0m\n");
|
||||||
|
printf("usage: sleep <ticks>\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
int ticks = atoi(argv[1]);
|
||||||
|
int ret = sleep(ticks);
|
||||||
|
if (ret) {
|
||||||
|
printf("\x1b[31mFailed to sleep\x1b[0m\n");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
@ -22,14 +22,6 @@ int getpid(void);
|
|||||||
char* sbrk(int);
|
char* sbrk(int);
|
||||||
int sleep(int);
|
int sleep(int);
|
||||||
int uptime(void);
|
int uptime(void);
|
||||||
#ifdef LAB_NET
|
|
||||||
int connect(uint32, uint16, uint16);
|
|
||||||
#endif
|
|
||||||
#ifdef LAB_PGTBL
|
|
||||||
int pgaccess(void *base, int len, void *mask);
|
|
||||||
// usyscall region
|
|
||||||
int ugetpid(void);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// ulib.c
|
// ulib.c
|
||||||
int stat(const char*, struct stat*);
|
int stat(const char*, struct stat*);
|
||||||
@ -47,4 +39,3 @@ void free(void*);
|
|||||||
int atoi(const char*);
|
int atoi(const char*);
|
||||||
int memcmp(const void *, const void *, uint);
|
int memcmp(const void *, const void *, uint);
|
||||||
void *memcpy(void *, const void *, uint);
|
void *memcpy(void *, const void *, uint);
|
||||||
int statistics(void*, int);
|
|
||||||
|
|||||||
@ -36,5 +36,3 @@ entry("getpid");
|
|||||||
entry("sbrk");
|
entry("sbrk");
|
||||||
entry("sleep");
|
entry("sleep");
|
||||||
entry("uptime");
|
entry("uptime");
|
||||||
entry("connect");
|
|
||||||
entry("pgaccess");
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
#include "kernel/types.h"
|
#include "kernel/types.h"
|
||||||
#include "kernel/stat.h"
|
#include "kernel/stat.h"
|
||||||
#include "kernel/fcntl.h"
|
|
||||||
#include "user/user.h"
|
#include "user/user.h"
|
||||||
|
|
||||||
char buf[512];
|
char buf[512];
|
||||||
@ -44,7 +43,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
for(i = 1; i < argc; i++){
|
for(i = 1; i < argc; i++){
|
||||||
if((fd = open(argv[i], O_RDONLY)) < 0){
|
if((fd = open(argv[i], 0)) < 0){
|
||||||
printf("wc: cannot open %s\n", argv[i]);
|
printf("wc: cannot open %s\n", argv[i]);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
59
user/xargs.c
Normal file
59
user/xargs.c
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include "kernel/param.h"
|
||||||
|
#include "kernel/types.h"
|
||||||
|
#include "user/user.h"
|
||||||
|
|
||||||
|
static inline int is_ws(const char ch) {
|
||||||
|
return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
int STDIN = 0;
|
||||||
|
char linebuf[1024];
|
||||||
|
char* p = linebuf;
|
||||||
|
char* _argv[MAXARG];
|
||||||
|
while (read(STDIN, p, 1) > 0) {
|
||||||
|
if (*p == '\n') {
|
||||||
|
int i;
|
||||||
|
int in_ws = 1;
|
||||||
|
for (i = 0; i < argc - 1; ++i) {
|
||||||
|
_argv[i] = argv[i + 1];
|
||||||
|
}
|
||||||
|
for (char* p0 = linebuf; p0 <= p; ++p0) {
|
||||||
|
if (is_ws(*p0)) {
|
||||||
|
if (!in_ws) {
|
||||||
|
in_ws = 1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
*p0 = '\0';
|
||||||
|
} else if (in_ws) {
|
||||||
|
in_ws = 0;
|
||||||
|
_argv[i] = p0;
|
||||||
|
}
|
||||||
|
if (i >= MAXARG) {
|
||||||
|
printf("warning: too many arguments");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*p = '\0';
|
||||||
|
_argv[i] = 0;
|
||||||
|
// for (int j = 0; j < i; ++ j) {
|
||||||
|
// printf("%s\n", _argv[j]);
|
||||||
|
// }
|
||||||
|
if (fork() == 0) {
|
||||||
|
exec(_argv[0], _argv);
|
||||||
|
exit(0);
|
||||||
|
} else {
|
||||||
|
p = linebuf;
|
||||||
|
while (wait(0) != -1)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
p++;
|
||||||
|
if (p >= linebuf + sizeof(linebuf)) {
|
||||||
|
printf("line buffer exceeded\n");
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
6
user/xargstest.sh
Normal file
6
user/xargstest.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
mkdir a
|
||||||
|
echo hello > a/b
|
||||||
|
mkdir c
|
||||||
|
echo hello > c/b
|
||||||
|
echo hello > b
|
||||||
|
find . b | xargs grep hello
|
||||||
Loading…
Reference in New Issue
Block a user