From 3808f903625f42f58aa95e43e3caca3efaa4d118 Mon Sep 17 00:00:00 2001
From: Sanjit Bhat <sanjit.bhat@gmail.com>
Date: Mon, 30 Oct 2023 14:39:28 -0500
Subject: lock: release lab

---
 kernel/stats.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
 create mode 100644 kernel/stats.c

(limited to 'kernel/stats.c')

diff --git a/kernel/stats.c b/kernel/stats.c
new file mode 100644
index 0000000..9659bb9
--- /dev/null
+++ b/kernel/stats.c
@@ -0,0 +1,69 @@
+#include <stdarg.h>
+
+#include "types.h"
+#include "param.h"
+#include "spinlock.h"
+#include "sleeplock.h"
+#include "fs.h"
+#include "file.h"
+#include "riscv.h"
+#include "defs.h"
+
+#define BUFSZ 4096
+static struct {
+  struct spinlock lock;
+  char buf[BUFSZ];
+  int sz;
+  int off;
+} stats;
+
+int statscopyin(char*, int);
+int statslock(char*, int);
+  
+int
+statswrite(int user_src, uint64 src, int n)
+{
+  return -1;
+}
+
+int
+statsread(int user_dst, uint64 dst, int n)
+{
+  int m;
+
+  acquire(&stats.lock);
+
+  if(stats.sz == 0) {
+#ifdef LAB_PGTBL
+    stats.sz = statscopyin(stats.buf, BUFSZ);
+#endif
+#ifdef LAB_LOCK
+    stats.sz = statslock(stats.buf, BUFSZ);
+#endif
+  }
+  m = stats.sz - stats.off;
+
+  if (m > 0) {
+    if(m > n)
+      m  = n;
+    if(either_copyout(user_dst, dst, stats.buf+stats.off, m) != -1) {
+      stats.off += m;
+    }
+  } else {
+    m = -1;
+    stats.sz = 0;
+    stats.off = 0;
+  }
+  release(&stats.lock);
+  return m;
+}
+
+void
+statsinit(void)
+{
+  initlock(&stats.lock, "stats");
+
+  devsw[STATS].read = statsread;
+  devsw[STATS].write = statswrite;
+}
+
-- 
cgit v1.2.3