Ejemplo n.º 1
0
fips186_gen::fips186_gen (u_int p, u_int iter) : seed (NULL), pbits (p)
{
  pbytes = p >> 3;
  num_p_hashes = DIV_ROUNDUP (pbytes, HASHSIZE);
  raw_psize = num_p_hashes * HASHSIZE;
  raw_p = New char[raw_psize];
  num_p_candidates = pbits << 2;  // shouldn't fail -- 4x expected # of trials
  
  seedsize = DIV_ROUNDUP (HASHSIZE, 8) + 1; // 8 bytes in a u_int64_t
  seed = New u_int64_t[seedsize];
  for (u_int i = 0; i < seedsize; i++)
    seed[i] = rnd.gethyper ();
}
Ejemplo n.º 2
0
static uint32_t
buffer_size_for_closure(struct wl_closure *closure)
{
	const struct wl_message *message = closure->message;
	int i, count;
	struct argument_details arg;
	const char *signature;
	uint32_t size, buffer_size = 0;

	signature = message->signature;
	count = arg_count_for_signature(signature);
	for (i = 0; i < count; i++) {
		signature = get_next_argument(signature, &arg);

		switch (arg.type) {
		case 'h':
			break;
		case 'u':
		case 'i':
		case 'f':
		case 'o':
		case 'n':
			buffer_size++;
			break;
		case 's':
			if (closure->args[i].s == NULL) {
				buffer_size++;
				break;
			}

			size = strlen(closure->args[i].s) + 1;
			buffer_size += 1 + DIV_ROUNDUP(size, sizeof(uint32_t));
			break;
		case 'a':
			if (closure->args[i].a == NULL) {
				buffer_size++;
				break;
			}

			size = closure->args[i].a->size;
			buffer_size += (1 + DIV_ROUNDUP(size, sizeof(uint32_t)));
			break;
		default:
			break;
		}
	}

	return buffer_size + 2;
}
int SIMD_Group_PFD_2::decodeExp(const T* src) {
	uint32_t *srcInt = (uint32_t*)src;
	m_exp8Num = *srcInt++;
	m_exp16Num = *srcInt++;
	m_exp32Num = *srcInt++;

	const int smallBlkSize = 16;
	uint32_t align8Num = m_exp8Num / smallBlkSize * smallBlkSize;
	uint32_t align16Num = m_exp16Num / smallBlkSize * smallBlkSize;
	uint32_t align32Num = m_exp32Num / smallBlkSize * smallBlkSize;

	uint32_t pad = 16;

	if (m_exp8Num > 0) {
		m_exp8Arr = new uint32_t[m_exp8Num + pad];
		simd_unpack_se8<uint32_t>(m_exp8Arr, srcInt, align8Num);
		const uint8_t *pExp8 = (uint8_t*)srcInt;
		for (int i= align8Num; i < m_exp8Num ; ++i) {
			m_exp8Arr[i] = pExp8[i];
		}
		srcInt += DIV_ROUNDUP(m_exp8Num, 4);
	}

	if (m_exp16Num > 0) {
		m_exp16Arr = new uint32_t[m_exp16Num + pad];
		simd_unpack_se16<uint32_t>(m_exp16Arr, srcInt, align16Num);
		const uint16_t *pExp16 = (uint16_t*)srcInt;
		for (int i= align16Num; i < m_exp16Num ; ++i) {
			m_exp16Arr[i] = pExp16[i];
		}
		srcInt += DIV_ROUNDUP(m_exp16Num, 2);
	}

	if (m_exp32Num > 0) {
		m_exp32Arr = new uint32_t[m_exp32Num + pad];
		simd_unpack_se32<uint32_t>(m_exp32Arr, srcInt, align32Num);
		for (int i= align32Num; i < m_exp32Num ; ++i) {
			m_exp32Arr[i] = srcInt[i];
		}
		srcInt += m_exp32Num;
	}

	srcInt += 3;	// skip pad
	return ((char*)srcInt) - src;
}
Ejemplo n.º 4
0
inline void ZMEMCPY(void *dest, uint64_t n) {
	ASSERT(dest != NULL);

	char *d = reinterpret_cast<char *>(dest);

	uint64_t num = DIV_ROUNDUP(n, 4);
	for (uint64_t i = 0; i < num; i++, d += 16)
		ZMEMCPY128(d);
}
int SIMD_Group_PFD_2::encodeExp(char *des, const T *dummy) {

	uint32_t *desInt = (uint32_t*)des;
	uint32_t exp8Num = m_8BitExpVec.size();
	uint32_t exp16Num = m_16BitExpVec.size();
	uint32_t exp32Num = m_32BitExpVec.size();

	*desInt++ = exp8Num;
	*desInt++ = exp16Num;
	*desInt++ = exp32Num;

	memset(desInt, 0, DIV_ROUNDUP(exp8Num, 4) + DIV_ROUNDUP(exp16Num, 2) + exp32Num + 3);

	const int smallBlkSize = 16;
	uint32_t align8Num = exp8Num / smallBlkSize * smallBlkSize;
	uint32_t align16Num = exp16Num / smallBlkSize * smallBlkSize;
	uint32_t align32Num = exp32Num / smallBlkSize * smallBlkSize;

	simd_pack_se8<uint32_t>(desInt, &m_8BitExpVec[0], align8Num);
	uint8_t *pExp8 = (uint8_t*)desInt;
	for (int i= align8Num; i < exp8Num ; ++i) {
		pExp8[i] = (uint8_t)m_8BitExpVec[i];
	}
	desInt += DIV_ROUNDUP(exp8Num, 4);

	simd_pack_se16<uint32_t>(desInt, &m_16BitExpVec[0], align16Num);
	uint16_t *pExp16 = (uint16_t*)desInt;
	for (int i= align16Num; i < exp16Num ; ++i) {
		pExp16[i] = (uint16_t)m_16BitExpVec[i];
	}
	desInt += DIV_ROUNDUP(exp16Num, 2);

	simd_pack_se32<uint32_t>(desInt, &m_32BitExpVec[0], align32Num);
	for (int i= align32Num; i < exp32Num ; ++i) {
		desInt[i] = m_32BitExpVec[i];
	}
	desInt += exp32Num;

	desInt += 3;	// pad for simd decode
	return ((char*)desInt) - des;
}
Ejemplo n.º 6
0
/*
==================================================================================
 Funtion	:setup_root_vnode
 Input		:struct super_block *sb
 		 < this super block >
 		 struct vnode *v_root
 		 < root vnode to set up >
 Output		:void
 Return		:void
 Description	:set up root vnode
==================================================================================
*/
LOCAL void setup_root_vnode(struct super_block *sb, struct vnode *v_root)
{
	v_root->v_size = PAGESIZE;
	v_root->v_blocks = DIV_ROUNDUP(v_root->v_size, RAMFS_BLOCK_SIZE);
	v_root->v_mode = S_IRUSR | S_IWUSR | S_IXUSR |
				S_IRGRP | S_IXGRP |
				S_IRWXO | S_IXOTH;
	v_root->v_mode |= S_IFDIR;
	v_root->v_uid = 0;
	v_root->v_gid = 0;
	v_root->v_opflags = 0;
	v_root->v_bytes = 0;
	v_root->v_blkbits = sb->s_blocksize_bits;
	v_root->v_nlink = 2;
	atomic_init(&v_root->v_count, 1);
	//v_root->v_op = 
	v_root->v_fops = &ramfs_dir_file_ope;
}
Ejemplo n.º 7
0
Archivo: fs.c Proyecto: darfux/jos
// Flush the contents of file f out to disk.
// Loop over all the blocks in file.
// Translate the file block number into a disk block number
// and then check whether that disk block is dirty.  If so, write it out.
//
// Hint: use file_map_block, block_is_dirty, and write_block.
void
file_flush(struct File *f)
{
	// LAB 5: Your code here.
	// panic("file_flush not implemented");
	int error;
	int filebno;
	uint32_t diskbno;

	int filebnum = DIV_ROUNDUP(f->f_size, BLKSIZE);
	
	for(filebno=0; filebno<filebnum; filebno++)
	{
		error = file_map_block(f, filebno, &diskbno, 0);
		if(error< 0) panic("file_map_block fail");

		if(block_is_dirty(diskbno)) write_block(diskbno);			
	}
}
Ejemplo n.º 8
0
/*
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
 Funtion	:vfs_stat64
 Input		:struct vnode *vnode
 		 < vnode to get its file status >
 		 struct stat64_i386 *buf
 		 < file status buffer to outpu >
 Output		:struct stat64_i386 *buf
 		 < file status buffer to outpu >
 Return		:int
 		 < result >
 Description	:get file status
_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
*/
EXPORT int vfs_stat64(struct vnode *vnode, struct stat64_i386 *buf)
{
	struct super_block *sb = vnode->v_sb;
	
	buf->st_dev		= sb->s_dev;
	buf->st_ino		= (ino_t)vnode->v_ino;
	buf->_st_ino		= (ino_t)vnode->v_ino;
	buf->st_mode		= vnode->v_mode;
	buf->st_nlink		= vnode->v_nlink;
	buf->st_uid		= vnode->v_uid;
	buf->st_gid		= vnode->v_gid;
	buf->st_rdev		= vnode->v_rdev;
	buf->st_size		= (off64_t)vnode->v_size;
	buf->st_atime		= vnode->v_atime.tv_sec;
	buf->st_atime_nsec	= 0;
	buf->st_mtime		= vnode->v_mtime.tv_sec;
	buf->st_mtime_nsec	= 0;
	buf->st_ctime		= vnode->v_ctime.tv_sec;
	buf->st_ctime_nsec	= 0;
	buf->st_blocks		= DIV_ROUNDUP(vnode->v_size, S_BLKSIZE);
	buf->st_blksize		= buf->st_blocks * S_BLKSIZE;

#if 0
	printf("st_dev:%d ", buf->st_dev);
	printf("st_ino:%d ", buf->st_ino);

	printf("st_mode:0x%08X ", buf->st_mode);
	printf("st_nlink:%d\n", buf->st_nlink);
	printf("st_uid:%d ", buf->st_uid);
	printf("st_gid:%d ", buf->st_gid);
	printf("st_rdev:%d\n", buf->st_rdev);
	printf("st_size:%d ", buf->st_size);
	printf("st_atime:%d ", buf->st_atime);
	printf("st_mtime:%d\n", buf->st_mtime);
	printf("st_ctime:%d ", buf->st_ctime);
	printf("st_blksize:%d ", buf->st_blksize);
	printf("st_blocks:%d\n", buf->st_blocks);

#endif
	return(0);
}
Ejemplo n.º 9
0
Archivo: fs.c Proyecto: darfux/jos
//=w=
//*lab5 ex6 challenge*
//modified for lab5 ex6 4MB challenge
//make the indirect blocks to be a link list
//so that we can expand the filebno space
int
file_block_walk(struct File *f, uint32_t filebno, uint32_t **ppdiskbno, bool alloc)
{
	int r;
	uint32_t *ptr;
	char *blk=NULL;
	
	int totalBlockNum = super->s_nblocks;
	int maxIndirect = DIV_ROUNDUP((totalBlockNum-NDIRECT), (INDRECTMOUNT+1));

	if(filebno < NDIRECT)
	{
		ptr = &f->f_direct[filebno];
	}
	else if(filebno < (NINDIRECT-NDIRECT)*maxIndirect+NDIRECT)
	{
		int fileIndNo = DIV_ROUNDUP(filebno-NDIRECT+1, INDRECTMOUNT);
		uint32_t* indirect = &(f->f_indirect);
		uint32_t* parentblk=NULL;
		int parentblkno=-1;
		//loop to find the owner of filebno
		for(; fileIndNo>0; fileIndNo--)
		{
			if ((*indirect) == 0)
			{
				if (alloc == 0) return -E_NOT_FOUND;
				if ((r = alloc_block()) < 0) return r;
				(*indirect) = r;
				if( indirect != &(f->f_indirect))
				{
					//if it's indirect block, 
					//refresh the next indirect block entry
					write_block(r);
				}
			}
			else
			{
				alloc = 0;	// we did not allocate a block
			}
			parentblk = (uint32_t*)blk;
			if ((r = read_block((*indirect), &blk)) < 0)
			{
				return r;
			}
			assert(blk != 0);
			if (alloc)		// must clear any block we allocated
			{
				memset(blk, 0, BLKSIZE);
			}
			//set the pointer to the next indirect block
			((uint32_t*)blk)[1] = parentblkno;
			parentblkno = (*indirect);
			indirect = (uint32_t*)blk;
		}
		ptr = (uint32_t*)blk + FILE_IND_BLKOFFSET(filebno);
	}
	else
	{
		return -E_INVAL;
	}

	*ppdiskbno = ptr;
	return 0;
}
Ejemplo n.º 10
0
struct wl_closure *
wl_connection_demarshal(struct wl_connection *connection,
			uint32_t size,
			struct wl_map *objects,
			const struct wl_message *message)
{
	uint32_t *p, *next, *end, length, id;
	int fd;
	char *s;
	unsigned int i, count, num_arrays;
	const char *signature;
	struct argument_details arg;
	struct wl_closure *closure;
	struct wl_array *array, *array_extra;

	count = arg_count_for_signature(message->signature);
	if (count > WL_CLOSURE_MAX_ARGS) {
		wl_log("too many args (%d)\n", count);
		errno = EINVAL;
		wl_connection_consume(connection, size);
		return NULL;
	}

	num_arrays = wl_message_count_arrays(message);
	closure = malloc(sizeof *closure + size + num_arrays * sizeof *array);
	if (closure == NULL) {
		errno = ENOMEM;
		wl_connection_consume(connection, size);
		return NULL;
	}

	array_extra = closure->extra;
	p = (uint32_t *)(closure->extra + num_arrays);
	end = p + size / sizeof *p;

	wl_connection_copy(connection, p, size);
	closure->sender_id = *p++;
	closure->opcode = *p++ & 0x0000ffff;

	signature = message->signature;
	for (i = 0; i < count; i++) {
		signature = get_next_argument(signature, &arg);

		if (arg.type != 'h' && p + 1 > end) {
			wl_log("message too short, "
			       "object (%d), message %s(%s)\n",
			       *p, message->name, message->signature);
			errno = EINVAL;
			goto err;
		}

		switch (arg.type) {
		case 'u':
			closure->args[i].u = *p++;
			break;
		case 'i':
			closure->args[i].i = *p++;
			break;
		case 'f':
			closure->args[i].f = *p++;
			break;
		case 's':
			length = *p++;

			if (length == 0) {
				closure->args[i].s = NULL;
				break;
			}

			next = p + DIV_ROUNDUP(length, sizeof *p);
			if (next > end) {
				wl_log("message too short, "
				       "object (%d), message %s(%s)\n",
				       closure->sender_id, message->name,
				       message->signature);
				errno = EINVAL;
				goto err;
			}

			s = (char *) p;

			if (length > 0 && s[length - 1] != '\0') {
				wl_log("string not nul-terminated, "
				       "message %s(%s)\n",
				       message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			closure->args[i].s = s;
			p = next;
			break;
		case 'o':
			id = *p++;
			closure->args[i].n = id;

			if (id == 0 && !arg.nullable) {
				wl_log("NULL object received on non-nullable "
				       "type, message %s(%s)\n", message->name,
				       message->signature);
				errno = EINVAL;
				goto err;
			}
			break;
		case 'n':
			id = *p++;
			closure->args[i].n = id;

			if (id == 0 && !arg.nullable) {
				wl_log("NULL new ID received on non-nullable "
				       "type, message %s(%s)\n", message->name,
				       message->signature);
				errno = EINVAL;
				goto err;
			}

			if (wl_map_reserve_new(objects, id) < 0) {
				wl_log("not a valid new object id (%u), "
				       "message %s(%s)\n",
				       id, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			break;
		case 'a':
			length = *p++;

			next = p + DIV_ROUNDUP(length, sizeof *p);
			if (next > end) {
				wl_log("message too short, "
				       "object (%d), message %s(%s)\n",
				       closure->sender_id, message->name,
				       message->signature);
				errno = EINVAL;
				goto err;
			}

			array_extra->size = length;
			array_extra->alloc = 0;
			array_extra->data = p;

			closure->args[i].a = array_extra++;
			p = next;
			break;
		case 'h':
			if (connection->fds_in.tail == connection->fds_in.head) {
				wl_log("file descriptor expected, "
				       "object (%d), message %s(%s)\n",
				       closure->sender_id, message->name,
				       message->signature);
				errno = EINVAL;
				goto err;
			}

			wl_buffer_copy(&connection->fds_in, &fd, sizeof fd);
			connection->fds_in.tail += sizeof fd;
			closure->args[i].h = fd;
			break;
		default:
			wl_abort("unknown type\n");
			break;
		}
	}

	closure->count = count;
	closure->message = message;

	wl_connection_consume(connection, size);

	return closure;

 err:
	wl_closure_destroy(closure);
	wl_connection_consume(connection, size);

	return NULL;
}
Ejemplo n.º 11
0
static int
serialize_closure(struct wl_closure *closure, uint32_t *buffer,
		  size_t buffer_count)
{
	const struct wl_message *message = closure->message;
	unsigned int i, count, size;
	uint32_t *p, *end;
	struct argument_details arg;
	const char *signature;

	if (buffer_count < 2)
		goto overflow;

	p = buffer + 2;
	end = buffer + buffer_count;

	signature = message->signature;
	count = arg_count_for_signature(signature);
	for (i = 0; i < count; i++) {
		signature = get_next_argument(signature, &arg);

		if (arg.type == 'h')
			continue;

		if (p + 1 > end)
			goto overflow;

		switch (arg.type) {
		case 'u':
			*p++ = closure->args[i].u;
			break;
		case 'i':
			*p++ = closure->args[i].i;
			break;
		case 'f':
			*p++ = closure->args[i].f;
			break;
		case 'o':
			*p++ = closure->args[i].o ? closure->args[i].o->id : 0;
			break;
		case 'n':
			*p++ = closure->args[i].n;
			break;
		case 's':
			if (closure->args[i].s == NULL) {
				*p++ = 0;
				break;
			}

			size = strlen(closure->args[i].s) + 1;
			*p++ = size;

			if (p + DIV_ROUNDUP(size, sizeof *p) > end)
				goto overflow;

			memcpy(p, closure->args[i].s, size);
			p += DIV_ROUNDUP(size, sizeof *p);
			break;
		case 'a':
			if (closure->args[i].a == NULL) {
				*p++ = 0;
				break;
			}

			size = closure->args[i].a->size;
			*p++ = size;

			if (p + DIV_ROUNDUP(size, sizeof *p) > end)
				goto overflow;

			memcpy(p, closure->args[i].a->data, size);
			p += DIV_ROUNDUP(size, sizeof *p);
			break;
		default:
			break;
		}
	}

	size = (p - buffer) * sizeof *p;

	buffer[0] = closure->sender_id;
	buffer[1] = size << 16 | (closure->opcode & 0x0000ffff);

	return size;

overflow:
	errno = ERANGE;
	return -1;
}
Ejemplo n.º 12
0
void *mem_searchrn(void *s, size_t len)
{
	vector unsigned char v_cr;
	vector unsigned char v_nl;
	vector unsigned char v0;
	vector unsigned char v_perm;
	vector unsigned char c;
	vector bool char rr, rn;
	vector bool char last_rr;
	char *p;
	ssize_t k;
	size_t block_num;
	unsigned f;

	if(unlikely(!s || !len))
		return NULL;

	/* only do one prefetch, this covers nearly 128k */
	block_num = DIV_ROUNDUP(len, 512);
	f  = block_num >= 256 ? 0 : block_num << 16;
	f |= 512;
	vec_dst((const unsigned char *)s, f, 2);

	v_cr = vec_splat_u8('\r');
	v_nl = vec_splat_u8('\n');
	v0   = vec_splat_u8(0);
	last_rr = (vector bool char)v0;

	k = SOVUC - ALIGN_DOWN_DIFF(s, SOVUC) - (ssize_t)len;

	p = (char *)ALIGN_DOWN(s, SOVUC);
	c = vec_ldl(0, (const vector unsigned char *)p);
	if(unlikely(k > 0))
		goto K_SHIFT;
	v_perm = vec_lvsl(0, (unsigned char *)s);
	c = vec_perm(c, v0, v_perm);
	v_perm = vec_lvsr(0, (unsigned char *)s);
	c = vec_perm(v0, c, v_perm);
	rr = vec_cmpeq(c, v_cr);
	rn = vec_cmpeq(c, v_nl);

	k = -k;
	goto START_LOOP;

	do
	{
		p += SOVUC;
		c = vec_ldl(0, (const vector unsigned char *)p);
		k -= SOVUC;
		if(k > 0)
		{
			rr = vec_cmpeq(c, v_cr);
			rn = vec_cmpeq(c, v_nl);

			if(vec_any_eq(last_rr, rn)) {
				vec_dss(2);
				return p - 1;
			}
START_LOOP:
			last_rr = (vector bool char)vec_sld(v0, (vector unsigned char)rr, 1);
			rn = (vector bool char)vec_sld(v0, (vector unsigned char)rn, 15);
			rr = vec_and(rr, rn); /* get mask */
			if(vec_any_ne(rr, v0)) {
				vec_dss(2);
				return p + vec_zpos(rr);
			}
		}
	} while(k > 0);
	k = -k;
K_SHIFT:
	vec_dss(2);
	v_perm = vec_lvsr(0, (unsigned char *)k);
	c = vec_perm(v0, c, v_perm);
	v_perm = vec_lvsl(0, (unsigned char *)k);
	c = vec_perm(c, v0, v_perm);
	rr = vec_cmpeq(c, v_cr);
	rn = vec_cmpeq(c, v_nl);
	if(vec_any_eq(last_rr, rn))
		return p - 1;

	rn = (vector bool char)vec_sld(v0, (vector unsigned char)rn, 15);
	rr = vec_and(rr, rn); /* get mask */
	if(vec_any_ne(rr, v0))
		return p + vec_zpos(rr);

	return NULL;
}
Ejemplo n.º 13
0
struct wl_closure *
wl_connection_demarshal(struct wl_connection *connection,
			uint32_t size,
			struct wl_map *objects,
			const struct wl_message *message)
{
	uint32_t *p, *next, *end, length, **id;
	int *fd;
	char *extra, **s;
	unsigned int i, count, extra_space;
	const char *signature = message->signature;
	struct argument_details arg;
	struct wl_object **object;
	struct wl_array **array;
	struct wl_closure *closure;

	count = arg_count_for_signature(signature) + 2;
	if (count > ARRAY_LENGTH(closure->types)) {
		printf("too many args (%d)\n", count);
		errno = EINVAL;
		wl_connection_consume(connection, size);
		return NULL;
	}

	extra_space = wl_message_size_extra(message);
	closure = malloc(sizeof *closure + 8 + size + extra_space);
	if (closure == NULL)
		return NULL;

	closure->message = message;
	closure->types[0] = &ffi_type_pointer;
	closure->types[1] = &ffi_type_pointer;
	closure->start = closure->buffer;

	wl_connection_copy(connection, closure->buffer, size);
	p = &closure->buffer[2];
	end = (uint32_t *) ((char *) p + size);
	extra = (char *) end;
	for (i = 2; i < count; i++) {
		signature = get_next_argument(signature, &arg);

		if (p + 1 > end) {
			printf("message too short, "
			       "object (%d), message %s(%s)\n",
			       *p, message->name, message->signature);
			errno = EINVAL;
			goto err;
		}

		switch (arg.type) {
		case 'u':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p++;
			break;
		case 'i':
			closure->types[i] = &ffi_type_sint32;
			closure->args[i] = p++;
			break;
		case 'f':
			closure->types[i] = &ffi_type_sint32;
			closure->args[i] = p++;
			break;
		case 's':
			closure->types[i] = &ffi_type_pointer;
			length = *p++;

			next = p + DIV_ROUNDUP(length, sizeof *p);
			if (next > end) {
				printf("message too short, "
				       "object (%d), message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			s = (char **) extra;
			extra += sizeof *s;
			closure->args[i] = s;

			if (length == 0) {
				*s = NULL;
			} else {
				*s = (char *) p;
			}

			if (length > 0 && (*s)[length - 1] != '\0') {
				printf("string not nul-terminated, "
				       "message %s(%s)\n",
				       message->name, message->signature);
				errno = EINVAL;
				goto err;
			}
			p = next;
			break;
		case 'o':
			closure->types[i] = &ffi_type_pointer;
			object = (struct wl_object **) extra;
			extra += sizeof *object;
			closure->args[i] = object;

			if (*p == 0 && !arg.nullable) {
				printf("NULL new ID received on non-nullable "
				       "type, message %s(%s)\n", message->name,
				       message->signature);
				*object = NULL;
				errno = EINVAL;
				goto err;
			}

			*object = wl_map_lookup(objects, *p);
			if (*object == WL_ZOMBIE_OBJECT) {
				/* references object we've already
				 * destroyed client side */
				*object = NULL;
			} else if (*object == NULL && *p != 0) {
				printf("unknown object (%u), message %s(%s)\n",
				       *p, message->name, message->signature);
				*object = NULL;
				errno = EINVAL;
				goto err;
			}

			if (*object != NULL && message->types[i-2] != NULL &&
			    (*object)->interface != message->types[i-2]) {
				printf("invalid object (%u), type (%s), "
					"message %s(%s)\n",
				       *p, (*object)->interface->name,
				       message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			p++;
			break;
		case 'n':
			closure->types[i] = &ffi_type_pointer;
			id = (uint32_t **) extra;
			extra += sizeof *id;
			closure->args[i] = id;
			*id = p;

			if (*id == 0 && !arg.nullable) {
				printf("NULL new ID received on non-nullable "
				       "type, message %s(%s)\n", message->name,
				       message->signature);
				errno = EINVAL;
				goto err;
			}

			if (wl_map_reserve_new(objects, *p) < 0) {
				printf("not a valid new object id (%d), "
				       "message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			p++;
			break;
		case 'a':
			closure->types[i] = &ffi_type_pointer;
			length = *p++;

			next = p + DIV_ROUNDUP(length, sizeof *p);
			if (next > end) {
				printf("message too short, "
				       "object (%d), message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			array = (struct wl_array **) extra;
			extra += sizeof *array;
			closure->args[i] = array;

			*array = (struct wl_array *) extra;
			extra += sizeof **array;

			(*array)->size = length;
			(*array)->alloc = 0;
			(*array)->data = p;
			p = next;
			break;
		case 'h':
			closure->types[i] = &ffi_type_sint;

			fd = (int *) extra;
			extra += sizeof *fd;
			closure->args[i] = fd;

			wl_buffer_copy(&connection->fds_in, fd, sizeof *fd);
			connection->fds_in.tail += sizeof *fd;
			break;
		default:
			printf("unknown type\n");
			assert(0);
			break;
		}
	}

	closure->count = i;

	ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
		     closure->count, &ffi_type_void, closure->types);

	wl_connection_consume(connection, size);

	return closure;

 err:
	closure->count = i;
	wl_closure_destroy(closure);
	wl_connection_consume(connection, size);

	return NULL;
}
Ejemplo n.º 14
0
struct wl_closure *
wl_closure_vmarshal(struct wl_object *sender,
		    uint32_t opcode, va_list ap,
		    const struct wl_message *message)
{
	struct wl_closure *closure;
	struct wl_object **objectp, *object;
	uint32_t length, aligned, *p, *start, size, *end;
	int dup_fd;
	struct wl_array **arrayp, *array;
	const char **sp, *s;
	const char *signature = message->signature;
	struct argument_details arg;
	char *extra;
	int i, count, fd, extra_size, *fd_ptr;

	/* FIXME: Match old fixed allocation for now */
	closure = malloc(sizeof *closure + 1024);
	if (closure == NULL)
		return NULL;

	extra_size = wl_message_size_extra(message);
	count = arg_count_for_signature(signature) + 2;
	extra = (char *) closure->buffer;
	start = &closure->buffer[DIV_ROUNDUP(extra_size, sizeof *p)];
	end = &closure->buffer[256];
	p = &start[2];

	closure->types[0] = &ffi_type_pointer;
	closure->types[1] = &ffi_type_pointer;

	for (i = 2; i < count; i++) {
		signature = get_next_argument(signature, &arg);

		switch (arg.type) {
		case 'f':
			closure->types[i] = &ffi_type_sint32;
			closure->args[i] = p;
			if (end - p < 1)
				goto err;
			*p++ = va_arg(ap, wl_fixed_t);
			break;
		case 'u':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p;
			if (end - p < 1)
				goto err;
			*p++ = va_arg(ap, uint32_t);
			break;
		case 'i':
			closure->types[i] = &ffi_type_sint32;
			closure->args[i] = p;
			if (end - p < 1)
				goto err;
			*p++ = va_arg(ap, int32_t);
			break;
		case 's':
			closure->types[i] = &ffi_type_pointer;
			closure->args[i] = extra;
			sp = (const char **) extra;
			extra += sizeof *sp;

			s = va_arg(ap, const char *);

			if (!arg.nullable && s == NULL)
				goto err_null;

			length = s ? strlen(s) + 1: 0;
			aligned = (length + 3) & ~3;
			if (p + aligned / sizeof *p + 1 > end)
				goto err;
			*p++ = length;

			if (length > 0)
				*sp = (const char *) p;
			else
				*sp = NULL;

			memcpy(p, s, length);
			memset((char *) p + length, 0, aligned - length);
			p += aligned / sizeof *p;
			break;
		case 'o':
			closure->types[i] = &ffi_type_pointer;
			closure->args[i] = extra;
			objectp = (struct wl_object **) extra;
			extra += sizeof *objectp;

			object = va_arg(ap, struct wl_object *);

			if (!arg.nullable && object == NULL)
				goto err_null;

			*objectp = object;
			if (end - p < 1)
				goto err;
			*p++ = object ? object->id : 0;
			break;

		case 'n':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p;
			object = va_arg(ap, struct wl_object *);
			if (end - p < 1)
				goto err;

			if (!arg.nullable && object == NULL)
				goto err_null;

			*p++ = object ? object->id : 0;
			break;

		case 'a':
			closure->types[i] = &ffi_type_pointer;
			closure->args[i] = extra;
			arrayp = (struct wl_array **) extra;
			extra += sizeof *arrayp;

			*arrayp = (struct wl_array *) extra;
			extra += sizeof **arrayp;

			array = va_arg(ap, struct wl_array *);

			if (!arg.nullable && array == NULL)
				goto err_null;

			if (array == NULL || array->size == 0) {
				if (end - p < 1)
					goto err;
				*p++ = 0;
				break;
			}
			if (p + DIV_ROUNDUP(array->size, sizeof *p) + 1 > end)
				goto err;
			*p++ = array->size;
			memcpy(p, array->data, array->size);

			(*arrayp)->size = array->size;
			(*arrayp)->alloc = array->alloc;
			(*arrayp)->data = p;

			p += DIV_ROUNDUP(array->size, sizeof *p);
			break;

		case 'h':
			closure->types[i] = &ffi_type_sint;
			closure->args[i] = extra;
			fd_ptr = (int *) extra;
			extra += sizeof *fd_ptr;

			fd = va_arg(ap, int);
			dup_fd = wl_os_dupfd_cloexec(fd, 0);
			if (dup_fd < 0) {
				fprintf(stderr, "dup failed: %m");
				abort();
			}
			*fd_ptr = dup_fd;
			break;
		default:
			fprintf(stderr, "unhandled format code: '%c'\n",
				arg.type);
			assert(0);
			break;
		}
	}

	size = (p - start) * sizeof *p;
	start[0] = sender->id;
	start[1] = opcode | (size << 16);

	closure->start = start;
	closure->message = message;
	closure->count = count;

	ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
		     closure->count, &ffi_type_void, closure->types);

	return closure;

err:
	printf("request too big to marshal, maximum size is %zu\n",
	       sizeof closure->buffer);
	errno = ENOMEM;

	return NULL;

err_null:
	free(closure);
	wl_log("error marshalling arguments for %s:%i.%s (signature %s): "
	       "null value passed for arg %i\n",
	       sender->interface->name, sender->id, message->name,
	       message->signature, i);
	errno = EINVAL;
	return NULL;
}
Ejemplo n.º 15
0
int libxl__arch_domain_create(libxl__gc *gc, libxl_domain_config *d_config,
        uint32_t domid)
{
    int ret = 0;
    int tsc_mode;
    uint32_t rtc_timeoffset;
    libxl_ctx *ctx = libxl__gc_owner(gc);

    if (d_config->b_info.type == LIBXL_DOMAIN_TYPE_PV)
        xc_domain_set_memmap_limit(ctx->xch, domid,
                                   (d_config->b_info.max_memkb +
                                    d_config->b_info.u.pv.slack_memkb));

    switch (d_config->b_info.tsc_mode) {
    case LIBXL_TSC_MODE_DEFAULT:
        tsc_mode = 0;
        break;
    case LIBXL_TSC_MODE_ALWAYS_EMULATE:
        tsc_mode = 1;
        break;
    case LIBXL_TSC_MODE_NATIVE:
        tsc_mode = 2;
        break;
    case LIBXL_TSC_MODE_NATIVE_PARAVIRT:
        tsc_mode = 3;
        break;
    default:
        abort();
    }
    xc_domain_set_tsc_info(ctx->xch, domid, tsc_mode, 0, 0, 0);
    if (libxl_defbool_val(d_config->b_info.disable_migrate))
        xc_domain_disable_migrate(ctx->xch, domid);
    rtc_timeoffset = d_config->b_info.rtc_timeoffset;
    if (libxl_defbool_val(d_config->b_info.localtime)) {
        time_t t;
        struct tm *tm, result;

        t = time(NULL);
        tm = localtime_r(&t, &result);

        if (!tm) {
            LOGED(ERROR, domid, "Failed to call localtime_r");
            ret = ERROR_FAIL;
            goto out;
        }

        rtc_timeoffset += tm->tm_gmtoff;
    }

    if (rtc_timeoffset)
        xc_domain_set_time_offset(ctx->xch, domid, rtc_timeoffset);

    if (d_config->b_info.type == LIBXL_DOMAIN_TYPE_HVM) {
        unsigned long shadow = DIV_ROUNDUP(d_config->b_info.shadow_memkb,
                                           1024);
        xc_shadow_control(ctx->xch, domid, XEN_DOMCTL_SHADOW_OP_SET_ALLOCATION,
                          NULL, 0, &shadow, 0, NULL);
    }

    if (d_config->c_info.type == LIBXL_DOMAIN_TYPE_PV &&
            libxl_defbool_val(d_config->b_info.u.pv.e820_host)) {
        ret = libxl__e820_alloc(gc, domid, d_config);
        if (ret) {
            LOGED(ERROR, domid, "Failed while collecting E820 with: %d (errno:%d)\n",
                 ret, errno);
        }
    }

out:
    return ret;
}
Ejemplo n.º 16
0
Archivo: fs.c Proyecto: darfux/jos
// Remove any blocks currently used by file 'f',
// but not necessary for a file of size 'newsize'.
// For both the old and new sizes, figure out the number of blocks required,
// and then clear the blocks from new_nblocks to old_nblocks.
// If the new_nblocks is no more than NDIRECT, and the indirect block has
// been allocated (f->f_indirect != 0), then free the indirect block too.
// (Remember to clear the f->f_indirect pointer so you'll know
// whether it's valid!)
// Do not change f->f_size.
static void
file_truncate_blocks(struct File *f, off_t newsize)
{
	int error;
	uint32_t old_nblocks, new_nblocks;

	// Hint: Use file_clear_block and/or free_block.
	// LAB 5: Your code here.
	// panic("file_truncate_blocks not implemented");
	int oldsize = f->f_size;


	// but not necessary for a file of size 'newsize'.
	if(newsize>oldsize) return;
	
	// For both the old and new sizes, figure out the number of blocks required,
	// and then clear the blocks from new_nblocks to old_nblocks.
	new_nblocks = DIV_ROUNDUP(newsize, BLKSIZE);
	old_nblocks = DIV_ROUNDUP(oldsize, BLKSIZE);
	
	int filebno;
	for(filebno=new_nblocks; filebno<old_nblocks; filebno++)
	{
		error = file_clear_block(f, filebno);
		if(error< 0) panic("free blocks fail");
	}

	// If the new_nblocks is no more than NDIRECT, and the indirect block has
	// been allocated (f->f_indirect != 0), then free the indirect block too.
	// (Remember to clear the f->f_indirect pointer so you'll know
	// whether it's valid!)

	// if(new_nblocks<=NDIRECT && old_nblocks>NDIRECT)
	// {
	// 	free_block(f->f_indirect);				
	// 	f->f_indirect = 0;
	// }
	//for ex6 4mb challenge
	int fileIndNoOld=0, fileIndNoNew=0;
	if(old_nblocks-NDIRECT>0)
	{
		fileIndNoOld = DIV_ROUNDUP(old_nblocks-NDIRECT, INDRECTMOUNT);
	}
	if(new_nblocks-NDIRECT>0)
	{
		fileIndNoNew = DIV_ROUNDUP(new_nblocks-NDIRECT, INDRECTMOUNT);
	}
	else
	{
		fileIndNoNew=0;
	}
	char* tmpblk;
	if ((error = read_block(f->f_indirect, &tmpblk) < 0))
	{
		panic("wrong in truncating %e\n", error);
	}

	int tmp = fileIndNoOld-1;
	while(tmp-->0)
	{
		if ((error = read_block(tmpblk[0], &tmpblk) < 0))
		{
			panic("wrong in truncating %e\n", error);
		}
	}
	int parent;
	char* parentblk;
	tmp = fileIndNoOld-fileIndNoNew;
	while(tmp-->0)
	{
		parent = ((int*)tmpblk)[1];
		if ((error = read_block(tmpblk[1], &parentblk) < 0))
		{
			panic("wrong in truncating %e\n", error);
		}
		if(parent!=-1)
		{//we set in block_walk
			free_block(parentblk[0]);	
		}
		else
		{//reach the tail
			free_block(f->f_indirect);
		}
		tmpblk = parentblk;
	}
}
Ejemplo n.º 17
0
struct wl_closure *
wl_connection_demarshal(struct wl_connection *connection,
			uint32_t size,
			struct wl_hash_table *objects,
			const struct wl_message *message)
{
	uint32_t *p, *next, *end, length;
	int *fd;
	char *extra, **s;
	int i, count, extra_space;
	struct wl_object **object;
	struct wl_array **array;
	struct wl_closure *closure = &connection->receive_closure;

	count = strlen(message->signature) + 2;
	if (count > ARRAY_LENGTH(closure->types)) {
		printf("too many args (%d)\n", count);
		assert(0);
	}

	extra_space = wl_message_size_extra(message);
	if (sizeof closure->buffer < size + extra_space) {
		printf("request too big, should malloc tmp buffer here\n");
		assert(0);
	}

	closure->message = message;
	closure->types[0] = &ffi_type_pointer;
	closure->types[1] = &ffi_type_pointer;

	wl_connection_copy(connection, closure->buffer, size);
	p = &closure->buffer[2];
	end = (uint32_t *) ((char *) (p + size));
	extra = (char *) end;
	for (i = 2; i < count; i++) {
		if (p + 1 > end) {
			printf("message too short, "
			       "object (%d), message %s(%s)\n",
			       *p, message->name, message->signature);
			errno = EINVAL;
			goto err;
		}

		switch (message->signature[i - 2]) {
		case 'u':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p++;
			break;
		case 'i':
			closure->types[i] = &ffi_type_sint32;
			closure->args[i] = p++;
			break;
		case 's':
			closure->types[i] = &ffi_type_pointer;
			length = *p++;

			next = p + DIV_ROUNDUP(length, sizeof *p);
			if (next > end) {
				printf("message too short, "
				       "object (%d), message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			s = (char **) extra;
			extra += sizeof *s;
			closure->args[i] = s;

			if (length == 0) {
				*s = NULL;
			} else {
				*s = (char *) p;
			}

			if (length > 0 && (*s)[length - 1] != '\0') {
				printf("string not nul-terminated, "
				       "message %s(%s)\n",
				       message->name, message->signature);
				errno = EINVAL;
				goto err;
			}
			p = next;
			break;
		case 'o':
			closure->types[i] = &ffi_type_pointer;
			object = (struct wl_object **) extra;
			extra += sizeof *object;
			closure->args[i] = object;

			*object = wl_hash_table_lookup(objects, *p);
			if (*object == NULL && *p != 0) {
				printf("unknown object (%d), message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			p++;
			break;
		case 'n':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p;
			object = wl_hash_table_lookup(objects, *p);
			if (object != NULL) {
				printf("not a new object (%d), "
				       "message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}
			p++;
			break;
		case 'a':
			closure->types[i] = &ffi_type_pointer;
			length = *p++;

			next = p + DIV_ROUNDUP(length, sizeof *p);
			if (next > end) {
				printf("message too short, "
				       "object (%d), message %s(%s)\n",
				       *p, message->name, message->signature);
				errno = EINVAL;
				goto err;
			}

			array = (struct wl_array **) extra;
			extra += sizeof *array;
			closure->args[i] = array;

			*array = (struct wl_array *) extra;
			extra += sizeof **array;

			(*array)->size = length;
			(*array)->alloc = 0;
			(*array)->data = p;
			p = next;
			break;
		case 'h':
			closure->types[i] = &ffi_type_sint;

			fd = (int *) extra;
			extra += sizeof *fd;
			closure->args[i] = fd;

			wl_buffer_copy(&connection->fds_in, fd, sizeof *fd);
			connection->fds_in.tail += sizeof *fd;
			break;
		default:
			printf("unknown type\n");
			assert(0);
			break;
		}
	}

	closure->count = i;
	ffi_prep_cif(&closure->cif, FFI_DEFAULT_ABI,
		     closure->count, &ffi_type_uint32, closure->types);

	wl_connection_consume(connection, size);

	return closure;

 err:
	closure->count = i;
	wl_closure_destroy(closure);

	return NULL;
}
Ejemplo n.º 18
0
struct wl_closure *
wl_connection_vmarshal(struct wl_connection *connection,
		       struct wl_object *sender,
		       uint32_t opcode, va_list ap,
		       const struct wl_message *message)
{
	struct wl_closure *closure = &connection->send_closure;
	struct wl_object **objectp, *object;
	uint32_t length, *p, *start, size;
	int dup_fd;
	struct wl_array **arrayp, *array;
	const char **sp, *s;
	char *extra;
	int i, count, fd, extra_size, *fd_ptr;

	extra_size = wl_message_size_extra(message);
	count = strlen(message->signature) + 2;
	extra = (char *) closure->buffer;
	start = &closure->buffer[DIV_ROUNDUP(extra_size, sizeof *p)];
	p = &start[2];
	for (i = 2; i < count; i++) {
		switch (message->signature[i - 2]) {
		case 'u':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p;
			*p++ = va_arg(ap, uint32_t);
			break;
		case 'i':
			closure->types[i] = &ffi_type_sint32;
			closure->args[i] = p;
			*p++ = va_arg(ap, int32_t);
			break;
		case 's':
			closure->types[i] = &ffi_type_pointer;
			closure->args[i] = extra;
			sp = (const char **) extra;
			extra += sizeof *sp;

			s = va_arg(ap, const char *);
			length = s ? strlen(s) + 1: 0;
			*p++ = length;

			if (length > 0)
				*sp = (const char *) p;
			else
				*sp = NULL;

			memcpy(p, s, length);
			p += DIV_ROUNDUP(length, sizeof *p);
			break;
		case 'o':
			closure->types[i] = &ffi_type_pointer;
			closure->args[i] = extra;
			objectp = (struct wl_object **) extra;
			extra += sizeof *objectp;

			object = va_arg(ap, struct wl_object *);
			*objectp = object;
			*p++ = object ? object->id : 0;
			break;

		case 'n':
			closure->types[i] = &ffi_type_uint32;
			closure->args[i] = p;
			object = va_arg(ap, struct wl_object *);
			*p++ = object->id;
			break;

		case 'a':
			closure->types[i] = &ffi_type_pointer;
			closure->args[i] = extra;
			arrayp = (struct wl_array **) extra;
			extra += sizeof *arrayp;

			*arrayp = (struct wl_array *) extra;
			extra += sizeof **arrayp;

			array = va_arg(ap, struct wl_array *);
			if (array == NULL || array->size == 0) {
				*p++ = 0;
				break;
			}
			*p++ = array->size;
			memcpy(p, array->data, array->size);

			(*arrayp)->size = array->size;
			(*arrayp)->alloc = array->alloc;
			(*arrayp)->data = p;

			p += DIV_ROUNDUP(array->size, sizeof *p);
			break;

		case 'h':
			closure->types[i] = &ffi_type_sint;
			closure->args[i] = extra;
			fd_ptr = (int *) extra;
			extra += sizeof *fd_ptr;

			fd = va_arg(ap, int);
			dup_fd = dup(fd);
			if (dup_fd < 0) {
				fprintf(stderr, "dup failed: %m");
				abort();
			}
			*fd_ptr = dup_fd;
			wl_buffer_put(&connection->fds_out,
				      &dup_fd, sizeof dup_fd);
			break;
		default:
			assert(0);
			break;
		}
	}

	size = (p - start) * sizeof *p;
	start[0] = sender->id;
	start[1] = opcode | (size << 16);

	closure->start = start;
	closure->message = message;
	closure->count = count;

	return closure;
}