Re: [akaros] [PATCH 4/4] capability device: get it to compile

2016-10-13 Thread barret rhoden
Hi -

Couple minor things:

> @@ -99,8 +85,8 @@ static int32_t capstat(struct chan *c, uint8_t *db, int32_t 
> n)
>  static struct chan *capopen(struct chan *c, int omode)
>  {
>   if (c->qid.type & QTDIR) {
> - if (omode != OREAD)
> - error(Ebadarg);
> + if (omode != O_RDONLY)
> + error(EISDIR, "Is a directory");
>   c->mode = omode;

I think we might want/need to run omode through openmode().  The other
drivers do something like:

kern/drivers/dev/mnt.c  c->mode = openmode(omode);

Technically, I guess we don't, since we error if omode isn't O_RDONLY.
(In which case, we can just as easily say c->mode = O_RDONLY).  

Btw, restricting flags to only O_RDONLY (a.k.a. O_READ) might have some
issues too, since the omode that comes in might have other flags set.
e.g. O_REMCLO, O_ASYNC, etc.  That's why openmode() filters omode with
O_ACCMODE.  I'm sure we can deal with those cases if they ever pop up.

> diff --git a/kern/include/env.h b/kern/include/env.h
> index 9dad848..dca5aee 100644
> --- a/kern/include/env.h
> +++ b/kern/include/env.h
> @@ -33,7 +33,7 @@ struct proc {
>   TAILQ_ENTRY(proc) sibling_link;
>   spinlock_t proc_lock;
>   struct user_context scp_ctx;/* context for an SCP.  TODO: move to 
> vc0 */
> - char user[64]; /* user name */
> + char *user;

I think this will break things elsewhere, since p->user is dereferenced
in a lot of places.  Easiest thing for now is to leave it at user[64],
and then in a later patch we dynamically allocate it.

What's your plan for that anyways?  Have 'user' point to some static
string by default, then dynamically allocate if we change the name?
(and then free in __proc_free())?


Otherwise, this patch set looks fine.  I didn't actually look at how
the cap device works - I just scanned for porting issues.  I figure
once we get it and sha256/crypto in and we think it all is working,
then I'll take a closer look.

Thanks,

Barret

-- 
You received this message because you are subscribed to the Google Groups 
"Akaros" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akaros+unsubscr...@googlegroups.com.
To post to this group, send email to akaros@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[akaros] [PATCH 3/3] crypto: get sha256 support to build.

2016-10-13 Thread Ronald G. Minnich
For now we'll just go with the sh256.c. That said,
we'll keep the other bits in here. Sooner or later we may
need the other crypto functions. Note these are not compiled
in conditionally.

We should consider removing the conditional compiling
of the unrolled code; we don't have space constraints of firmware.

Change-Id: Ic792cf2b89fa4f01a94c420eb3c620b62c7bf2a9
Signed-off-by: Ronald G. Minnich 
---
 kern/include/crypto/2common.h   |  4 +---
 kern/include/crypto/2crc8.h |  4 +---
 kern/include/crypto/2crypto.h   |  4 +---
 kern/include/crypto/2fw_hash_tags.h |  4 +---
 kern/include/crypto/2hmac.h |  3 +--
 kern/include/crypto/2id.h   |  4 +---
 kern/include/crypto/2misc.h |  4 +---
 kern/include/crypto/2nvstorage.h|  4 +---
 kern/include/crypto/2nvstorage_fields.h |  4 +---
 kern/include/crypto/2recovery_reasons.h |  4 +---
 kern/include/crypto/2rsa.h  |  4 +---
 kern/include/crypto/2secdata.h  |  4 +---
 kern/include/crypto/2sha.h  |  4 +---
 kern/include/crypto/2struct.h   |  4 +---
 kern/include/crypto/2sysincludes.h  | 35 -
 kern/include/crypto/2tpm_bootmode.h |  4 +---
 kern/lib/Kbuild |  1 +
 kern/lib/crypto/2sha256.c   |  6 +++---
 18 files changed, 40 insertions(+), 61 deletions(-)

diff --git a/kern/include/crypto/2common.h b/kern/include/crypto/2common.h
index 4622ab1..46c9a36 100644
--- a/kern/include/crypto/2common.h
+++ b/kern/include/crypto/2common.h
@@ -5,8 +5,7 @@
  * Common functions between firmware and kernel verified boot.
  */
 
-#ifndef VBOOT_REFERENCE_VBOOT_2COMMON_H_
-#define VBOOT_REFERENCE_VBOOT_2COMMON_H_
+#pragma once
 
 #include "2api.h"
 #include "2return_codes.h"
@@ -194,4 +193,3 @@ const struct vb2_id *vb2_hash_id(enum vb2_hash_algorithm 
hash_alg);
 /* Size of work buffer sufficient for vb2_verify_fw_preamble() worst case. */
 #define VB2_VERIFY_FIRMWARE_PREAMBLE_WORKBUF_BYTES 
VB2_VERIFY_DATA_WORKBUF_BYTES
 
-#endif  /* VBOOT_REFERENCE_VBOOT_2COMMON_H_ */
diff --git a/kern/include/crypto/2crc8.h b/kern/include/crypto/2crc8.h
index f01eabd..a846e5a 100644
--- a/kern/include/crypto/2crc8.h
+++ b/kern/include/crypto/2crc8.h
@@ -5,8 +5,7 @@
  * Very simple 8-bit CRC function.
  */
 
-#ifndef VBOOT_REFERENCE_2_CRC8_H_
-#define VBOOT_REFERENCE_2_CRC8_H_
+#pragma once
 
 /**
  * Calculate CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial.
@@ -17,4 +16,3 @@
  */
 uint8_t vb2_crc8(const void *data, uint32_t size);
 
-#endif /* VBOOT_REFERENCE_2_CRC8_H_ */
diff --git a/kern/include/crypto/2crypto.h b/kern/include/crypto/2crypto.h
index c1f225d..461c459 100644
--- a/kern/include/crypto/2crypto.h
+++ b/kern/include/crypto/2crypto.h
@@ -5,8 +5,7 @@
  * Crypto constants for verified boot
  */
 
-#ifndef VBOOT_REFERENCE_VBOOT_2CRYPTO_H_
-#define VBOOT_REFERENCE_VBOOT_2CRYPTO_H_
+#pragma once
 #include 
 
 /* Verified boot crypto algorithms */
@@ -62,4 +61,3 @@ enum vb2_hash_algorithm {
VB2_HASH_ALG_COUNT,
 };
 
-#endif /* VBOOT_REFERENCE_VBOOT_2CRYPTO_H_ */
diff --git a/kern/include/crypto/2fw_hash_tags.h 
b/kern/include/crypto/2fw_hash_tags.h
index 0c061f5..d6e366a 100644
--- a/kern/include/crypto/2fw_hash_tags.h
+++ b/kern/include/crypto/2fw_hash_tags.h
@@ -5,8 +5,7 @@
  * Firmware hash tags for verified boot
  */
 
-#ifndef VBOOT_REFERENCE_VBOOT_2FW_HASH_TAGS_H_
-#define VBOOT_REFERENCE_VBOOT_2FW_HASH_TAGS_H_
+#pragma once
 #include 
 
 /*
@@ -37,4 +36,3 @@ enum vb2_hash_tag {
VB2_HASH_TAG_CALLER_BASE = 0x4000
 };
 
-#endif /* VBOOT_REFERENCE_VBOOT_2FW_HASH_TAGS_H_ */
diff --git a/kern/include/crypto/2hmac.h b/kern/include/crypto/2hmac.h
index 1df1939..7209a38 100644
--- a/kern/include/crypto/2hmac.h
+++ b/kern/include/crypto/2hmac.h
@@ -3,8 +3,7 @@
  * found in the LICENSE file.
  */
 
-#ifndef VBOOT_REFERENCE_VBOOT_2HMAC_H_
-#define VBOOT_REFERENCE_VBOOT_2HMAC_H_
+#pragma once
 
 #include 
 #include "2crypto.h"
diff --git a/kern/include/crypto/2id.h b/kern/include/crypto/2id.h
index 03f6d96..12f5059 100644
--- a/kern/include/crypto/2id.h
+++ b/kern/include/crypto/2id.h
@@ -8,8 +8,7 @@
  * resistant to collisions and easy to compare.
  */
 
-#ifndef VBOOT_REFERENCE_VBOOT_2ID_H_
-#define VBOOT_REFERENCE_VBOOT_2ID_H_
+#pragma once
 #include 
 
 #define VB2_ID_NUM_BYTES 20
@@ -25,4 +24,3 @@ struct vb2_id {
 #define VB2_ID_NONE_SHA256 {{0x02, 0x56,}}
 #define VB2_ID_NONE_SHA512 {{0x05, 0x12,}}
 
-#endif  /* VBOOT_REFERENCE_VBOOT_2ID_H_ */
diff --git a/kern/include/crypto/2misc.h b/kern/include/crypto/2misc.h
index f562fad..fdc252d 100644
--- a/kern/include/crypto/2misc.h
+++ b/kern/include/crypto/2misc.h
@@ -5,8 +5,7 @@
  * Misc functions which need access to vb2_context but are not public APIs
  */
 
-#ifndef VBOOT_REFERENCE_VBOOT_2MISC_H_
-#define VBOOT_REFERENCE_VBOOT_2MISC_H_
+#pragma once
 
 #include "2api.h"
 
@@ -163,4 +162,3 @@ int 

[akaros] [PATCH] AHCI: get it to build and almost work.

2016-10-13 Thread Fergus Simpson
From: "Ronald G. Minnich" 

In qemu, it still shows the device as having zero bytes.
But it does find it.

Change-Id: I81262b460a9cd43a848c1d782c109ec216afb795
Signed-off-by: Ronald G. Minnich 
Signed-off-by: Fergus Simpson 
---
 kern/drivers/dev/Kbuild|   3 +
 kern/drivers/dev/sd.c  | 699 +--
 kern/drivers/dev/sdiahci.c | 805 +++--
 kern/drivers/dev/sdscsi.c  |  76 +++--
 kern/include/ahci.h|   6 +-
 kern/include/sd.h  | 150 -
 6 files changed, 920 insertions(+), 819 deletions(-)

diff --git a/kern/drivers/dev/Kbuild b/kern/drivers/dev/Kbuild
index 183bddd..ff89f35 100644
--- a/kern/drivers/dev/Kbuild
+++ b/kern/drivers/dev/Kbuild
@@ -12,6 +12,9 @@ obj-y += proc.o
 obj-y  += random.o
 obj-$(CONFIG_REGRESS)  += regress.o
 obj-y  += root.o
+obj-y  += sd.o
+obj-y  += sdscsi.o
+obj-y  += sdiahci.o
 obj-y  += srv.o
 obj-y  += version.o
 obj-$(CONFIG_DEVVARS)  += vars.o
diff --git a/kern/drivers/dev/sd.c b/kern/drivers/dev/sd.c
index 412bc78..da2b0c1 100644
--- a/kern/drivers/dev/sd.c
+++ b/kern/drivers/dev/sd.c
@@ -10,28 +10,42 @@
 /*
  * Storage Device.
  */
-#include "../port/error.h"
-#include "../port/lib.h"
-#include "dat.h"
-#include "fns.h"
-#include "io.h"
-#include "mem.h"
-#include "u.h"
-#include "ureg.h"
 
-#include "../port/sd.h"
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
 
 extern struct dev sddevtab;
-extern struct sdifc *sdifc[];
+struct sdifc sdiahciifc;
+
+/* In Plan 9, this array is auto-generated. That's almost certainly not
+ * necessary;
+ * we can use linker sets at some point, as we do elsewhere in Akaros. */
+struct sdifc *sdifc[] = {
+, NULL,
+};
+
+static const char Echange[] = "media or partition has changed";
 
-static char Echange[] = "media or partition has changed";
+static const char devletters[] =
+"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
-static char devletters[] = "0123456789"
-   "abcdefghijklmnopqrstuvwxyz"
-   "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+static struct sdev *devs[sizeof(devletters) - 1];
 
-static struct sdev *devs[sizeof devletters - 1];
-static qlock_t devslock;
+static qlock_t devslock = QLOCK_INITIALIZER(devslock);
 
 enum {
Rawcmd,
@@ -90,7 +104,7 @@ void sdaddpart(struct sdunit *unit, char *name, uint64_t 
start, uint64_t end)
 * Check name not already used
 * and look for a free slot.
 */
-   if (unit->part != nil) {
+   if (unit->part != NULL) {
partno = -1;
for (i = 0; i < unit->npart; i++) {
pp = >part[i];
@@ -99,31 +113,34 @@ void sdaddpart(struct sdunit *unit, char *name, uint64_t 
start, uint64_t end)
partno = i;
break;
}
-   if (strcmp(name, pp->SDperm.name) == 0) {
+   if (strcmp(name, pp->sdperm.name) == 0) {
if (pp->start == start && pp->end == end)
return;
-   error(Ebadctl);
+   error(EINVAL, "%s: '%s' is not valid", 
__func__, name);
}
}
} else {
-   if ((unit->part = malloc(sizeof(struct sdpart) * SDnpart)) == 
nil)
-   error(Enomem);
+   unit->part = kzmalloc(sizeof(struct sdpart) * SDnpart, 0);
+   if (unit->part == NULL)
+   error(ENOMEM, "%s: can't allocate %d bytes", __func__,
+ sizeof(struct sdpart) * SDnpart);
unit->npart = SDnpart;
partno = 0;
}
 
/*
 * If no free slot found then increase the
-* array size (can't get here with unit->part == nil).
+* array size (can't get here with unit->part == NULL).
 */
if (partno == -1) {
if (unit->npart >= NPart)
-   error(Enomem);
-   if ((pp = malloc(sizeof(struct sdpart) * (unit->npart + 
SDnpart))) ==
-   nil)
-   error(Enomem);
+   error(ENOMEM, "%s: no memory", __func__);
+   pp = kzmalloc(sizeof(struct sdpart) * (unit->npart + SDnpart), 
0);
+   if (pp == NULL)
+   

[akaros] [PATCH 2/4] capability: clang-format the capability device

2016-10-13 Thread Ronald G. Minnich
Change-Id: I3e99b8317fc57fbfb775fd4242e5fb2f36411a46
Signed-off-by: Ronald G. Minnich 
---
 kern/drivers/dev/capability.c | 181 +++---
 1 file changed, 81 insertions(+), 100 deletions(-)

diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c
index 53a59af..a3bafd7 100644
--- a/kern/drivers/dev/capability.c
+++ b/kern/drivers/dev/capability.c
@@ -7,79 +7,68 @@
  * in the LICENSE file.
  */
 
-#include   "u.h"
-#include   "../port/lib.h"
-#include   "mem.h"
-#include   "dat.h"
-#include   "fns.h"
-#include   "../port/error.h"
-
-#include   
-
-enum
-{
-   Hashlen=SHA1dlen,
-   Maxhash=256,
+#include "../port/error.h"
+#include "../port/lib.h"
+#include "dat.h"
+#include "fns.h"
+#include "mem.h"
+#include "u.h"
+
+#include 
+
+enum {
+   Hashlen = SHA1dlen,
+   Maxhash = 256,
 };
 
 /*
  *  if a process knows cap->cap, it can change user
  *  to capabilty->user.
  */
-typedef struct Caphash Caphash;
-struct Caphash
-{
-   Caphash *next;
-   charhash[Hashlen];
+typedef struct Caphash Caphash;
+struct Caphash {
+   Caphash *next;
+   char hash[Hashlen];
 };
 
-struct
-{
+struct {
QLock QLock;
-   Caphash *first;
-   int nhash;
+   Caphash *first;
+   int nhash;
 } capalloc;
 
-enum
-{
+enum {
Qdir,
Qhash,
Quse,
 };
 
 /* caphash must be last */
-Dirtab capdir[] =
-{
-   ".",{Qdir,0,QTDIR}, 0,  DMDIR|0500,
-   "capuse",   {Quse}, 0,  0222,
-   "caphash",  {Qhash},0,  0200,
+Dirtab capdir[] = {
+".",   {Qdir, 0, QTDIR}, 0, DMDIR | 0500, "capuse", {Quse}, 0, 0222,
+"caphash", {Qhash},  0, 0200,
 };
 int ncapdir = nelem(capdir);
 
-static Chan*
-capattach(char *spec)
+static Chan *capattach(char *spec)
 {
return devattach(L'¤', spec);
 }
 
-static Walkqid*
-capwalk(Chan *c, Chan *nc, char **name, int nname)
+static Walkqid *capwalk(Chan *c, Chan *nc, char **name, int nname)
 {
return devwalk(c, nc, name, nname, capdir, ncapdir, devgen);
 }
 
-static void
-capremove(Chan *c)
+static void capremove(Chan *c)
 {
-   if(iseve() && c->qid.path == Qhash)
-   ncapdir = nelem(capdir)-1;
+   if (iseve() && c->qid.path == Qhash)
+   ncapdir = nelem(capdir) - 1;
else
error(Eperm);
 }
 
-
-static int32_t
-capstat(Chan *c, uint8_t *db, int32_t n)
+static int32_t capstat(Chan *c, uint8_t *db, int32_t n)
 {
return devstat(c, db, n, capdir, ncapdir, devgen);
 }
@@ -87,11 +76,10 @@ capstat(Chan *c, uint8_t *db, int32_t n)
 /*
  *  if the stream doesn't exist, create it
  */
-static Chan*
-capopen(Chan *c, int omode)
+static Chan *capopen(Chan *c, int omode)
 {
-   if(c->qid.type & QTDIR){
-   if(omode != OREAD)
+   if (c->qid.type & QTDIR) {
+   if (omode != OREAD)
error(Ebadarg);
c->mode = omode;
c->flag |= COPEN;
@@ -99,9 +87,9 @@ capopen(Chan *c, int omode)
return c;
}
 
-   switch((uint32_t)c->qid.path){
+   switch ((uint32_t)c->qid.path) {
case Qhash:
-   if(!iseve())
+   if (!iseve())
error(Eperm);
break;
}
@@ -116,32 +104,31 @@ capopen(Chan *c, int omode)
 static char*
 hashstr(uchar *hash)
 {
-   static char buf[2*Hashlen+1];
-   int i;
+static char buf[2*Hashlen+1];
+int i;
 
-   for(i = 0; i < Hashlen; i++)
-   sprint(buf+2*i, "%2.2x", hash[i]);
-   buf[2*Hashlen] = 0;
-   return buf;
+for(i = 0; i < Hashlen; i++)
+sprint(buf+2*i, "%2.2x", hash[i]);
+buf[2*Hashlen] = 0;
+return buf;
 }
  */
 
-static Caphash*
-remcap(uint8_t *hash)
+static Caphash *remcap(uint8_t *hash)
 {
Caphash *t, **l;
 
qlock();
 
/* find the matching capability */
-   for(l =  *l != nil;){
+   for (l =  *l != nil;) {
t = *l;
-   if(memcmp(hash, t->hash, Hashlen) == 0)
+   if (memcmp(hash, t->hash, Hashlen) == 0)
break;
l = >next;
}
t = *l;
-   if(t != nil){
+   if (t != nil) {
capalloc.nhash--;
*l = t->next;
}
@@ -151,8 +138,7 @@ remcap(uint8_t *hash)
 }
 
 /* add a capability, throwing out any old ones */
-static void
-addcap(uint8_t *hash)
+static void addcap(uint8_t *hash)
 {
Caphash *p, *t, **l;
 
@@ -163,9 +149,9 @@ addcap(uint8_t *hash)
qlock();
 
/* trim extras */
-   while(capalloc.nhash >= Maxhash){
+   while (capalloc.nhash >= Maxhash) {
t = capalloc.first;
-   if(t == nil)
+   if (t == nil)
panic("addcap");

[akaros] [PATCH 4/4] capability device: get it to compile

2016-10-13 Thread Ronald G. Minnich
We need to do something about the use of sha1.

Change-Id: I80795609ccea1ac629cb7b9d4a95040cc040d76a
Signed-off-by: Ronald G. Minnich 
---
 kern/drivers/dev/Kbuild   |   1 +
 kern/drivers/dev/capability.c | 155 --
 kern/include/env.h|   2 +-
 kern/include/ns.h |   2 +-
 4 files changed, 77 insertions(+), 83 deletions(-)

diff --git a/kern/drivers/dev/Kbuild b/kern/drivers/dev/Kbuild
index 183bddd..9ea2b05 100644
--- a/kern/drivers/dev/Kbuild
+++ b/kern/drivers/dev/Kbuild
@@ -1,5 +1,6 @@
 obj-y  += acpi.o
 obj-y  += alarm.o
+obj-y  += capability.o
 obj-y  += coreboot.o
 obj-y  += cons.o
 obj-y  += ether.o
diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c
index 9bd487c..bb4fcdf 100644
--- a/kern/drivers/dev/capability.c
+++ b/kern/drivers/dev/capability.c
@@ -21,22 +21,8 @@
 #include 
 #include 
 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-#include 
-
 enum {
-   Hashlen = SHA1dlen,
+   Hashlen = 20, // SHA1dlen,
Maxhash = 256,
 };
 
@@ -44,15 +30,14 @@ enum {
  *  if a process knows cap->cap, it can change user
  *  to capabilty->user.
  */
-typedef struct Caphash Caphash;
 struct Caphash {
-   Caphash *next;
+   struct Caphash *next;
char hash[Hashlen];
 };
 
 struct {
-   qlock_t qlock_t qlock;
-   Caphash *first;
+   qlock_t qlock;
+   struct Caphash *first;
int nhash;
 } capalloc;
 
@@ -64,18 +49,19 @@ enum {
 
 /* caphash must be last */
 struct dirtab capdir[] = {
-".",   {Qdir, 0, QTDIR}, 0, DMDIR | 0500, "capuse", {Quse}, 0, 0222,
-"caphash", {Qhash},  0, 0200,
+   {".",   {Qdir, 0, QTDIR}, 0, DMDIR | 0500},
+   {"capuse",  {Quse}, 0, 0222,},
+   {"caphash", {Qhash},  0, 0200,},
 };
 int ncapdir = ARRAY_SIZE(capdir);
 
 static struct chan *capattach(char *spec)
 {
-   return devattach(L'¤', spec);
+   return devattach("capability", spec);
 }
 
 static struct walkqid *capwalk(struct chan *c, struct chan *nc, char **name,
-   int nname)
+   int nname)
 {
return devwalk(c, nc, name, nname, capdir, ncapdir, devgen);
 }
@@ -85,7 +71,7 @@ static void capremove(struct chan *c)
if (iseve() && c->qid.path == Qhash)
ncapdir = ARRAY_SIZE(capdir) - 1;
else
-   error(Eperm);
+   error(EPERM, "Permission denied");
 }
 
 static int32_t capstat(struct chan *c, uint8_t *db, int32_t n)
@@ -99,8 +85,8 @@ static int32_t capstat(struct chan *c, uint8_t *db, int32_t n)
 static struct chan *capopen(struct chan *c, int omode)
 {
if (c->qid.type & QTDIR) {
-   if (omode != OREAD)
-   error(Ebadarg);
+   if (omode != O_RDONLY)
+   error(EISDIR, "Is a directory");
c->mode = omode;
c->flag |= COPEN;
c->offset = 0;
@@ -110,7 +96,7 @@ static struct chan *capopen(struct chan *c, int omode)
switch ((uint32_t)c->qid.path) {
case Qhash:
if (!iseve())
-   error(Eperm);
+   error(EPERM, "Permission denied: only eve() can open 
Qhash");
break;
}
 
@@ -121,24 +107,24 @@ static struct chan *capopen(struct chan *c, int omode)
 }
 
 /*
-static char*
-hashstr(uint8_t *hash)
+  static char*
+  hashstr(uint8_t *hash)
+  {
+  static char buf[2*Hashlen+1];
+  int i;
+
+  for(i = 0; i < Hashlen; i++)
+  sprint(buf+2*i, "%2.2x", hash[i]);
+  buf[2*Hashlen] = 0;
+  return buf;
+  }
+*/
+
+static struct Caphash *remcap(uint8_t *hash)
 {
-static char buf[2*Hashlen+1];
-int i;
+   struct Caphash *t, **l;
 
-for(i = 0; i < Hashlen; i++)
-sprint(buf+2*i, "%2.2x", hash[i]);
-buf[2*Hashlen] = 0;
-return buf;
-}
- */
-
-static Caphash *remcap(uint8_t *hash)
-{
-   Caphash *t, **l;
-
-   qlock(&()->qlock);
+   qlock();
 
/* find the matching capability */
for (l =  *l != NULL;) {
@@ -152,21 +138,21 @@ static Caphash *remcap(uint8_t *hash)
capalloc.nhash--;
*l = t->next;
}
-   qunlock(&()->qlock);
-
+   qunlock();
+   
return t;
 }
 
 /* add a capability, throwing out any old ones */
 static void addcap(uint8_t *hash)
 {
-   Caphash *p, *t, **l;
+   struct Caphash *p, *t, **l;
 
p = kzmalloc(sizeof *p, 0);
memmove(p->hash, hash, Hashlen);
p->next = NULL;
 
-   qlock(&()->qlock);
+   qlock();
 
/* trim extras */
while 

[akaros] [PATCH 3/4] capability: run scripts/PLAN9 on capability device

2016-10-13 Thread Ronald G. Minnich
Change-Id: I55dbed3e636730c4768c61168f22c61c9e2c82fb
Signed-off-by: Ronald G. Minnich 
---
 kern/drivers/dev/capability.c | 108 +-
 1 file changed, 64 insertions(+), 44 deletions(-)

diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c
index a3bafd7..9bd487c 100644
--- a/kern/drivers/dev/capability.c
+++ b/kern/drivers/dev/capability.c
@@ -7,14 +7,33 @@
  * in the LICENSE file.
  */
 
-#include "../port/error.h"
-#include "../port/lib.h"
-#include "dat.h"
-#include "fns.h"
-#include "mem.h"
-#include "u.h"
-
-#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
 
 enum {
Hashlen = SHA1dlen,
@@ -32,7 +51,7 @@ struct Caphash {
 };
 
 struct {
-   QLock QLock;
+   qlock_t qlock_t qlock;
Caphash *first;
int nhash;
 } capalloc;
@@ -44,31 +63,32 @@ enum {
 };
 
 /* caphash must be last */
-Dirtab capdir[] = {
+struct dirtab capdir[] = {
 ".",   {Qdir, 0, QTDIR}, 0, DMDIR | 0500, "capuse", {Quse}, 0, 0222,
 "caphash", {Qhash},  0, 0200,
 };
-int ncapdir = nelem(capdir);
+int ncapdir = ARRAY_SIZE(capdir);
 
-static Chan *capattach(char *spec)
+static struct chan *capattach(char *spec)
 {
return devattach(L'¤', spec);
 }
 
-static Walkqid *capwalk(Chan *c, Chan *nc, char **name, int nname)
+static struct walkqid *capwalk(struct chan *c, struct chan *nc, char **name,
+   int nname)
 {
return devwalk(c, nc, name, nname, capdir, ncapdir, devgen);
 }
 
-static void capremove(Chan *c)
+static void capremove(struct chan *c)
 {
if (iseve() && c->qid.path == Qhash)
-   ncapdir = nelem(capdir) - 1;
+   ncapdir = ARRAY_SIZE(capdir) - 1;
else
error(Eperm);
 }
 
-static int32_t capstat(Chan *c, uint8_t *db, int32_t n)
+static int32_t capstat(struct chan *c, uint8_t *db, int32_t n)
 {
return devstat(c, db, n, capdir, ncapdir, devgen);
 }
@@ -76,7 +96,7 @@ static int32_t capstat(Chan *c, uint8_t *db, int32_t n)
 /*
  *  if the stream doesn't exist, create it
  */
-static Chan *capopen(Chan *c, int omode)
+static struct chan *capopen(struct chan *c, int omode)
 {
if (c->qid.type & QTDIR) {
if (omode != OREAD)
@@ -102,7 +122,7 @@ static Chan *capopen(Chan *c, int omode)
 
 /*
 static char*
-hashstr(uchar *hash)
+hashstr(uint8_t *hash)
 {
 static char buf[2*Hashlen+1];
 int i;
@@ -118,21 +138,21 @@ static Caphash *remcap(uint8_t *hash)
 {
Caphash *t, **l;
 
-   qlock();
+   qlock(&()->qlock);
 
/* find the matching capability */
-   for (l =  *l != nil;) {
+   for (l =  *l != NULL;) {
t = *l;
if (memcmp(hash, t->hash, Hashlen) == 0)
break;
l = >next;
}
t = *l;
-   if (t != nil) {
+   if (t != NULL) {
capalloc.nhash--;
*l = t->next;
}
-   qunlock();
+   qunlock(&()->qlock);
 
return t;
 }
@@ -142,36 +162,36 @@ static void addcap(uint8_t *hash)
 {
Caphash *p, *t, **l;
 
-   p = smalloc(sizeof *p);
+   p = kzmalloc(sizeof *p, 0);
memmove(p->hash, hash, Hashlen);
-   p->next = nil;
+   p->next = NULL;
 
-   qlock();
+   qlock(&()->qlock);
 
/* trim extras */
while (capalloc.nhash >= Maxhash) {
t = capalloc.first;
-   if (t == nil)
+   if (t == NULL)
panic("addcap");
capalloc.first = t->next;
-   free(t);
+   kfree(t);
capalloc.nhash--;
}
 
/* add new one */
-   for (l =  *l != nil; l = &(*l)->next)
+   for (l =  *l != NULL; l = &(*l)->next)
;
*l = p;
capalloc.nhash++;
 
-   qunlock();
+   qunlock(&()->qlock);
 }
 
-static void capclose(Chan *c)
+static void capclose(struct chan *c)
 {
 }
 
-static int32_t capread(Chan *c, void *va, int32_t n, int64_t m)
+static int32_t capread(struct chan *c, void *va, int32_t n, int64_t m)
 {
switch ((uint32_t)c->qid.path) {
case Qdir:
@@ -184,14 +204,14 @@ static int32_t capread(Chan *c, void *va, int32_t n, 
int64_t m)
return n;
 }
 
-static int32_t capwrite(Chan *c, void *va, int32_t n, int64_t m)
+static int32_t capwrite(struct chan *c, void *va, int32_t n, int64_t m)
 {
Caphash *p;
char *cp;
uint8_t hash[Hashlen];
char *key, *from, *to;
char err[256];
-   Proc *up = externup();
+   struct proc *up = externup();
 
switch ((uint32_t)c->qid.path) {
case Qhash:
@@ -205,33 +225,33 @@ static 

[akaros] [PATCH 1/4] add the capability device from Harvey (from Plan 9)

2016-10-13 Thread Ronald G. Minnich
Change-Id: If159e72517809eedd0d1e98271e3dde57e035090
Signed-off-by: Ronald G. Minnich 
---
 kern/drivers/dev/capability.c | 295 ++
 1 file changed, 295 insertions(+)
 create mode 100644 kern/drivers/dev/capability.c

diff --git a/kern/drivers/dev/capability.c b/kern/drivers/dev/capability.c
new file mode 100644
index 000..53a59af
--- /dev/null
+++ b/kern/drivers/dev/capability.c
@@ -0,0 +1,295 @@
+/*
+ * This file is part of the UCB release of Plan 9. It is subject to the license
+ * terms in the LICENSE file found in the top-level directory of this
+ * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
+ * part of the UCB release of Plan 9, including this file, may be copied,
+ * modified, propagated, or distributed except according to the terms contained
+ * in the LICENSE file.
+ */
+
+#include   "u.h"
+#include   "../port/lib.h"
+#include   "mem.h"
+#include   "dat.h"
+#include   "fns.h"
+#include   "../port/error.h"
+
+#include   
+
+enum
+{
+   Hashlen=SHA1dlen,
+   Maxhash=256,
+};
+
+/*
+ *  if a process knows cap->cap, it can change user
+ *  to capabilty->user.
+ */
+typedef struct Caphash Caphash;
+struct Caphash
+{
+   Caphash *next;
+   charhash[Hashlen];
+};
+
+struct
+{
+   QLock QLock;
+   Caphash *first;
+   int nhash;
+} capalloc;
+
+enum
+{
+   Qdir,
+   Qhash,
+   Quse,
+};
+
+/* caphash must be last */
+Dirtab capdir[] =
+{
+   ".",{Qdir,0,QTDIR}, 0,  DMDIR|0500,
+   "capuse",   {Quse}, 0,  0222,
+   "caphash",  {Qhash},0,  0200,
+};
+int ncapdir = nelem(capdir);
+
+static Chan*
+capattach(char *spec)
+{
+   return devattach(L'¤', spec);
+}
+
+static Walkqid*
+capwalk(Chan *c, Chan *nc, char **name, int nname)
+{
+   return devwalk(c, nc, name, nname, capdir, ncapdir, devgen);
+}
+
+static void
+capremove(Chan *c)
+{
+   if(iseve() && c->qid.path == Qhash)
+   ncapdir = nelem(capdir)-1;
+   else
+   error(Eperm);
+}
+
+
+static int32_t
+capstat(Chan *c, uint8_t *db, int32_t n)
+{
+   return devstat(c, db, n, capdir, ncapdir, devgen);
+}
+
+/*
+ *  if the stream doesn't exist, create it
+ */
+static Chan*
+capopen(Chan *c, int omode)
+{
+   if(c->qid.type & QTDIR){
+   if(omode != OREAD)
+   error(Ebadarg);
+   c->mode = omode;
+   c->flag |= COPEN;
+   c->offset = 0;
+   return c;
+   }
+
+   switch((uint32_t)c->qid.path){
+   case Qhash:
+   if(!iseve())
+   error(Eperm);
+   break;
+   }
+
+   c->mode = openmode(omode);
+   c->flag |= COPEN;
+   c->offset = 0;
+   return c;
+}
+
+/*
+static char*
+hashstr(uchar *hash)
+{
+   static char buf[2*Hashlen+1];
+   int i;
+
+   for(i = 0; i < Hashlen; i++)
+   sprint(buf+2*i, "%2.2x", hash[i]);
+   buf[2*Hashlen] = 0;
+   return buf;
+}
+ */
+
+static Caphash*
+remcap(uint8_t *hash)
+{
+   Caphash *t, **l;
+
+   qlock();
+
+   /* find the matching capability */
+   for(l =  *l != nil;){
+   t = *l;
+   if(memcmp(hash, t->hash, Hashlen) == 0)
+   break;
+   l = >next;
+   }
+   t = *l;
+   if(t != nil){
+   capalloc.nhash--;
+   *l = t->next;
+   }
+   qunlock();
+
+   return t;
+}
+
+/* add a capability, throwing out any old ones */
+static void
+addcap(uint8_t *hash)
+{
+   Caphash *p, *t, **l;
+
+   p = smalloc(sizeof *p);
+   memmove(p->hash, hash, Hashlen);
+   p->next = nil;
+
+   qlock();
+
+   /* trim extras */
+   while(capalloc.nhash >= Maxhash){
+   t = capalloc.first;
+   if(t == nil)
+   panic("addcap");
+   capalloc.first = t->next;
+   free(t);
+   capalloc.nhash--;
+   }
+
+   /* add new one */
+   for(l =  *l != nil; l = &(*l)->next)
+   ;
+   *l = p;
+   capalloc.nhash++;
+
+   qunlock();
+}
+
+static void
+capclose(Chan* c)
+{
+}
+
+static int32_t
+capread(Chan *c, void *va, int32_t n, int64_t m)
+{
+   switch((uint32_t)c->qid.path){
+   case Qdir:
+   return devdirread(c, va, n, capdir, ncapdir, devgen);
+
+   default:
+   error(Eperm);
+   break;
+   }
+   return n;
+}
+
+static int32_t
+capwrite(Chan *c, void *va, int32_t n, int64_t m)
+{
+   Caphash *p;
+   char *cp;
+   uint8_t hash[Hashlen];
+   char *key, *from, *to;
+   char err[256];
+   Proc *up = externup();
+
+   switch((uint32_t)c->qid.path){
+   case Qhash:
+   if(!iseve())
+