Skip to content

Commit

Permalink
changed the vma table from static to dynamically allocated array and …
Browse files Browse the repository at this point in the history
…removed the option VMA_REGIONS
  • Loading branch information
mikaku committed Dec 8, 2022
1 parent bf6b681 commit c786079
Show file tree
Hide file tree
Showing 10 changed files with 322 additions and 263 deletions.
115 changes: 63 additions & 52 deletions fs/procfs/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,19 +544,23 @@ int data_proc_pid_exe(char *buffer, __pid_t pid)
if((p = get_proc_by_pid(pid))) {

/* kernel and zombie processes are programless */
if(!p->vma || !p->vma->inode) {

/*
* This assumes that the first entry in the vma_table
* contains the program's inode.
*/
if(!p->vma_table || !p->vma_table->inode) {
return -ENOENT;
}

i = p->vma->inode;
i = p->vma_table->inode;
size = sprintk(buffer, "[%02d%02d]:%d", MAJOR(i->rdev), MINOR(i->rdev), i->inode);
}
return size;
}

int data_proc_pid_maps(char *buffer, __pid_t pid)
{
unsigned int n;
int size, len;
__ino_t inode;
int major, minor;
Expand All @@ -567,11 +571,8 @@ int data_proc_pid_maps(char *buffer, __pid_t pid)

size = 0;
if((p = get_proc_by_pid(pid))) {
if(!p->vma) {
return 0;
}
vma = p->vma;
for(n = 0; n < VMA_REGIONS && vma->start; n++, vma++) {
vma = p->vma_table;
while(vma) {
r = vma->prot & PROT_READ ? 'r' : '-';
w = vma->prot & PROT_WRITE ? 'w' : '-';
x = vma->prot & PROT_EXEC ? 'x' : '-';
Expand Down Expand Up @@ -609,8 +610,10 @@ int data_proc_pid_maps(char *buffer, __pid_t pid)
}
len = sprintk(buffer + size, "%08x-%08x %c%c%c%c %08x %02d:%02d %- 10u [%s]\n", vma->start, vma->end, r, w, x, f, vma->offset, major, minor, inode, section);
size += len;
vma = vma->next;
}
}

return size;
}

Expand Down Expand Up @@ -660,7 +663,7 @@ int data_proc_pid_root(char *buffer, __pid_t pid)

int data_proc_pid_stat(char *buffer, __pid_t pid)
{
int n, size, vma_start, vma_end;
int size, vma_start, vma_end;
unsigned int esp, eip;
int signum, mask;
__sigset_t sigignored, sigcaught;
Expand All @@ -671,14 +674,19 @@ int data_proc_pid_stat(char *buffer, __pid_t pid)

size = text = data = stack = mmap = 0;
if((p = get_proc_by_pid(pid))) {
if(!p->vma) {
vma = p->vma_table;
if(!vma) {
return 0;
}
vma_start = p->vma[0].start;
vma_end = p->vma[0].end;

vma = p->vma;
for(n = 0; n < VMA_REGIONS && vma->start; n++, vma++) {
/*
* This assumes that the first entry in the vma_table
* contains the program's inode.
*/
vma_start = vma->start;
vma_end = vma->end;

while(vma) {
switch(vma->s_type) {
case P_TEXT:
text += vma->end - vma->start;
Expand All @@ -694,6 +702,7 @@ int data_proc_pid_stat(char *buffer, __pid_t pid)
mmap += vma->end - vma->start;
break;
}
vma = vma->next;
}

sigignored = sigcaught = 0;
Expand All @@ -713,55 +722,55 @@ int data_proc_pid_stat(char *buffer, __pid_t pid)
eip = sc->eip;
}
size = sprintk(buffer, "%d (%s) %c %d %d %d %d %d %d %d %d %d %d %u %u %u %u %d %d %d %d %d %d %u %u %u %u %u %u %u %d %d %u %u %u\n",
p->pid,
p->argv0,
pstate[p->state][0],
p->ppid, p->pgid, p->sid,
p->ctty ? p->ctty->dev : 0,
p->ctty ? p->ctty->pgid : - 1,
0, /* flags */
0, 0, 0, 0, /* minflt, cminflt, majflt, cmajflt */
tv2ticks(&p->usage.ru_utime),
tv2ticks(&p->usage.ru_stime),
tv2ticks(&p->cusage.ru_utime),
tv2ticks(&p->cusage.ru_stime),
0, /* counter */
0, /* priority */
0, /* timeout */
0, /* itrealvalue */
p->start_time,
text + data + stack + mmap,
p->rss,
0x7FFFFFFF, /* rlim */
vma_start, /* startcode */
vma_end, /* endcode */
PAGE_OFFSET - 1, /* startstack */
esp, /* kstkesp */
eip, /* kstkeip */
p->sigpending,
p->sigblocked,
sigignored,
sigcaught,
p->sleep_address
p->pid,
p->argv0,
pstate[p->state][0],
p->ppid, p->pgid, p->sid,
p->ctty ? p->ctty->dev : 0,
p->ctty ? p->ctty->pgid : - 1,
0, /* flags */
0, 0, 0, 0, /* minflt, cminflt, majflt, cmajflt */
tv2ticks(&p->usage.ru_utime),
tv2ticks(&p->usage.ru_stime),
tv2ticks(&p->cusage.ru_utime),
tv2ticks(&p->cusage.ru_stime),
0, /* counter */
0, /* priority */
0, /* timeout */
0, /* itrealvalue */
p->start_time,
text + data + stack + mmap,
p->rss,
0x7FFFFFFF, /* rlim */
vma_start, /* startcode */
vma_end, /* endcode */
PAGE_OFFSET - 1, /* startstack */
esp, /* kstkesp */
eip, /* kstkeip */
p->sigpending,
p->sigblocked,
sigignored,
sigcaught,
p->sleep_address
);
}
return size;
}

int data_proc_pid_statm(char *buffer, __pid_t pid)
{
int n, size;
int size;
struct proc *p;
struct vma *vma;
int text, data, stack, mmap;

size = text = data = stack = mmap = 0;
if((p = get_proc_by_pid(pid))) {
if(!p->vma) {
vma = p->vma_table;
if(!vma) {
return 0;
}
vma = p->vma;
for(n = 0; n < VMA_REGIONS && vma->start; n++, vma++) {
while(vma) {
switch(vma->s_type) {
case P_TEXT:
text += vma->end - vma->start;
Expand All @@ -777,6 +786,7 @@ int data_proc_pid_statm(char *buffer, __pid_t pid)
mmap += vma->end - vma->start;
break;
}
vma = vma->next;
}

size = sprintk(buffer, "%d", (text + data + stack + mmap) / PAGE_SIZE);
Expand All @@ -792,7 +802,7 @@ int data_proc_pid_statm(char *buffer, __pid_t pid)

int data_proc_pid_status(char *buffer, __pid_t pid)
{
int n, size;
int size;
int signum, mask;
__sigset_t sigignored, sigcaught;
struct proc *p;
Expand All @@ -801,11 +811,11 @@ int data_proc_pid_status(char *buffer, __pid_t pid)

size = text = data = stack = mmap = 0;
if((p = get_proc_by_pid(pid))) {
if(!p->vma) {
vma = p->vma_table;
if(!vma) {
return 0;
}
vma = p->vma;
for(n = 0; n < VMA_REGIONS && vma->start; n++, vma++) {
while(vma) {
switch(vma->s_type) {
case P_TEXT:
text += vma->end - vma->start;
Expand All @@ -821,6 +831,7 @@ int data_proc_pid_status(char *buffer, __pid_t pid)
mmap += vma->end - vma->start;
break;
}
vma = vma->next;
}

size = sprintk(buffer, "Name:\t%s\n", p->argv0);
Expand Down
12 changes: 8 additions & 4 deletions fs/procfs/symlink.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* fiwix/fs/procfs/symlink.c
*
* Copyright 2018-2021, Jordi Sanfeliu. All rights reserved.
* Copyright 2018-2022, Jordi Sanfeliu. All rights reserved.
* Distributed under the terms of the Fiwix License.
*/

Expand Down Expand Up @@ -123,11 +123,15 @@ int procfs_followlink(struct inode *dir, struct inode *i, struct inode **i_res)
iput(i);
break;
case PROC_PID_EXE:
if(!p->vma || !p->vma->inode) {
/*
* This assumes that the first entry in the vma_table
* contains the program's inode.
*/
if(!p->vma_table || !p->vma_table->inode) {
return -ENOENT;
}
*i_res = p->vma->inode;
p->vma->inode->count++;
*i_res = p->vma_table->inode;
p->vma_table->inode->count++;
iput(i);
break;
case PROC_PID_ROOT:
Expand Down
1 change: 0 additions & 1 deletion include/fiwix/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
scroll back */
#define MAX_SPU_NOTICES 10 /* max. number of messages on spurious
interrupts */
#define VMA_REGIONS 150 /* max. number of virtual memory maps */


/* toggle configuration options */
Expand Down
6 changes: 4 additions & 2 deletions include/fiwix/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ struct vma {
char prot; /* PROT_READ, PROT_WRITE, ... */
unsigned int flags; /* MAP_SHARED, MAP_PRIVATE, ... */
unsigned int offset;
char s_type; /* section type (P_TEXT, P_DATA, ...) */
char s_type; /* segment type (P_TEXT, P_DATA, ...) */
struct inode *inode; /* file inode */
char o_mode; /* open mode (O_RDONLY, O_RDWR, ...) */
void *object; /* generic pointer (currently only for shm) */
struct vma *prev;
struct vma *next;
};

#include <fiwix/config.h>
Expand Down Expand Up @@ -132,7 +134,7 @@ struct proc {
int envc;
char **envp;
char pidstr[5]; /* PID number converted to string */
struct vma vma[VMA_REGIONS]; /* virtual memory-map addresses */
struct vma *vma_table; /* virtual memory-map addresses */
unsigned int brk_lower; /* lower limit of the heap section */
unsigned int brk; /* current limit of the heap */
__sigset_t sigpending;
Expand Down
1 change: 0 additions & 1 deletion kernel/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ void init_init(void)
memcpy_b(pgdir, kpage_dir, PAGE_SIZE);
init->tss.cr3 = V2P((unsigned int)pgdir);

memset_b(init->vma, 0, sizeof(init->vma));
init->ppid = 0;
init->pgid = 0;
init->sid = 0;
Expand Down
44 changes: 23 additions & 21 deletions kernel/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,35 @@ static int verify_address(int type, const void *addr, unsigned int size)
unsigned int start;

/*
* Verifies if the 'vma' array of that process is not empty. It can
* only be empty during the initialization of INIT, when it calls to
* sys_execve and sys_open without having yet a proper setup.
* The vma_table of the INIT process is not setup yet when it
* calls sys_open() and sys_execve() from init_trampoline(),
* but these calls are trusted.
*/
if(current->vma[0].s_type != 0) {
start = (unsigned int)addr;
if(!(vma = find_vma_region(start))) {
return -EFAULT;
}
if(!current->vma_table) {
return 0;
}

for(;;) {
if(type == VERIFY_WRITE) {
if(!(vma->prot & PROT_WRITE)) {
return -EFAULT;
}
} else {
if(!(vma->prot & PROT_READ)) {
return -EFAULT;
}
}
if(start + size < vma->end) {
break;
start = (unsigned int)addr;
if(!(vma = find_vma_region(start))) {
return -EFAULT;
}

for(;;) {
if(type == VERIFY_WRITE) {
if(!(vma->prot & PROT_WRITE)) {
return -EFAULT;
}
if(!(vma = find_vma_region(vma->end))) {
} else {
if(!(vma->prot & PROT_READ)) {
return -EFAULT;
}
}
if(start + size < vma->end) {
break;
}
if(!(vma = find_vma_region(vma->end))) {
return -EFAULT;
}
}
#endif /* CONFIG_LAZY_USER_ADDR_CHECK */

Expand Down
Loading

0 comments on commit c786079

Please sign in to comment.