Skip to content

Commit

Permalink
[kernel] Add fmemalloc sys call, fix fsck on 65M disks
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Jun 10, 2022
1 parent 850f549 commit 975c6d8
Show file tree
Hide file tree
Showing 27 changed files with 130 additions and 42 deletions.
3 changes: 3 additions & 0 deletions elks/arch/i86/drivers/char/console-bios.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <linuxmt/types.h>
#include <linuxmt/config.h>
#include <linuxmt/mm.h>
#include <linuxmt/sched.h>
#include <linuxmt/string.h>
#include <linuxmt/errno.h>
#include <linuxmt/chqueue.h>
#include <linuxmt/ntty.h>
#include "console.h"
Expand Down
3 changes: 3 additions & 0 deletions elks/arch/i86/drivers/char/console-direct-pc98.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <linuxmt/types.h>
#include <linuxmt/config.h>
#include <linuxmt/mm.h>
#include <linuxmt/sched.h>
#include <linuxmt/string.h>
#include <linuxmt/errno.h>
#include <linuxmt/chqueue.h>
#include <linuxmt/ntty.h>
#include <arch/io.h>
Expand Down
3 changes: 3 additions & 0 deletions elks/arch/i86/drivers/char/console-direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
#include <linuxmt/types.h>
#include <linuxmt/config.h>
#include <linuxmt/mm.h>
#include <linuxmt/sched.h>
#include <linuxmt/string.h>
#include <linuxmt/errno.h>
#include <linuxmt/chqueue.h>
#include <linuxmt/ntty.h>
#include <arch/io.h>
Expand Down
1 change: 1 addition & 0 deletions elks/arch/i86/drivers/char/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <linuxmt/fs.h>
#include <linuxmt/errno.h>
#include <linuxmt/mm.h>
#include <linuxmt/sched.h>
#include <linuxmt/debug.h>
#include <linuxmt/mem.h>
#include <linuxmt/heap.h>
Expand Down
1 change: 1 addition & 0 deletions elks/arch/i86/drivers/char/ntty.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <linuxmt/fcntl.h>
#include <linuxmt/errno.h>
#include <linuxmt/mm.h>
#include <linuxmt/string.h>
#include <linuxmt/termios.h>
#include <linuxmt/chqueue.h>
#include <linuxmt/ntty.h>
Expand Down
1 change: 1 addition & 0 deletions elks/arch/i86/drivers/net/ne2k.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <linuxmt/sched.h>
#include <linuxmt/limits.h>
#include <linuxmt/mm.h>
#include <linuxmt/string.h>
#include <linuxmt/debug.h>
#include <linuxmt/netstat.h>

Expand Down
1 change: 1 addition & 0 deletions elks/arch/i86/kernel/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <linuxmt/debug.h>
#include <linuxmt/kernel.h>
#include <linuxmt/mm.h>
#include <linuxmt/string.h>
#include <linuxmt/sched.h>
#include <linuxmt/signal.h>
#include <linuxmt/types.h>
Expand Down
1 change: 1 addition & 0 deletions elks/arch/i86/kernel/strace.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ struct syscall_info elks_table[] = {
{ "connect", packinfo(3, P_SSHORT, P_PDATA, P_SSHORT ) },
{ "setsockopt", packinfo(5, P_SSHORT, P_SSHORT, P_SSHORT ) }, /* +2 args*/
{ "getsocknam", packinfo(4, P_SSHORT, P_DATA, P_PUSHORT) }, /* +1 arg*/
{ "fmemalloc", packinfo(2, P_USHORT, P_PUSHORT, P_NONE) },
};

#endif
1 change: 1 addition & 0 deletions elks/arch/i86/kernel/syscall.dat
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ accept +202 3 = CONFIG_SOCKET
connect +203 3 = CONFIG_SOCKET
setsockopt +204 5 = CONFIG_SOCKET
getsocknam +205 4 = CONFIG_SOCKET
fmemalloc +206 2 *
#
# Name No Args Flag&comment
#
Expand Down
1 change: 1 addition & 0 deletions elks/arch/i86/kernel/timer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <linuxmt/config.h>
#include <linuxmt/mm.h>
#include <linuxmt/sched.h>
#include <linuxmt/timer.h>
#include <linuxmt/ntty.h>
#include <linuxmt/fixedpt.h>
Expand Down
39 changes: 37 additions & 2 deletions elks/arch/i86/mm/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <linuxmt/types.h>
#include <linuxmt/sched.h>
#include <linuxmt/mm.h>
//#include <linuxmt/mem.h>
#include <linuxmt/errno.h>
#include <linuxmt/debug.h>
#include <linuxmt/heap.h>

Expand Down Expand Up @@ -45,6 +45,7 @@ static segment_s * seg_split (segment_s * s1, segext_t size0)
s2->size = size2;
s2->flags = SEG_FLAG_FREE;
s2->ref_count = 0;
s2->pid = 0;

list_insert_after (&s1->all, &s2->all);
list_insert_after (&s1->free, &s2->free);
Expand Down Expand Up @@ -107,6 +108,7 @@ static void seg_merge (segment_s * s1, segment_s * s2)
{
list_remove (&s2->all);
s1->size += s2->size;
s1->pid = 0;
heap_free (s2);
}

Expand Down Expand Up @@ -150,6 +152,7 @@ void seg_free (segment_s * seg)
seg = prev;
} else {
seg->flags = SEG_FLAG_FREE;
seg->pid = 0;
}
}

Expand Down Expand Up @@ -280,6 +283,38 @@ int sys_sbrk (int increment, __u16 * pbrk)
return 0;
}

// allocate memory for process, return segment
int sys_fmemalloc(int paras, unsigned short *pseg)
{
segment_s *seg;
int err;

err = verify_area(VERIFY_WRITE, pseg, sizeof(*pseg));
if (err) return err;
seg = seg_alloc((segext_t)paras, SEG_FLAG_PROG);
if (!seg) return -ENOMEM;
seg->pid = current->pid;
put_user(seg->base, pseg);
return 0;
}

// free all program allocated segments for PID pid
void seg_free_pid(pid_t pid)
{
list_s *n;

again:
for (n = _seg_all.next; n != &_seg_all; ) {
segment_s * seg = structof (n, segment_s, all);

if (seg->pid == pid) {
seg_free(seg);
goto again; /* free may have changed linked list */
}
n = seg->all.next;
}
}


// Initialize the memory manager.

Expand All @@ -294,9 +329,9 @@ void INITPROC mm_init(seg_t start, seg_t end)
seg->size = end - start;
seg->flags = SEG_FLAG_FREE;
seg->ref_count = 0;
seg->pid = 0;

list_insert_before (&_seg_all, &(seg->all)); // add tail
list_insert_before (&_seg_free, &(seg->free)); // add tail
}
}

1 change: 1 addition & 0 deletions elks/fs/file_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linuxmt/stat.h>
#include <linuxmt/string.h>
#include <linuxmt/mm.h>
#include <linuxmt/errno.h>

/*
* first_file points to a doubly linked list of all file structures in
Expand Down
1 change: 1 addition & 0 deletions elks/fs/minix/symlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <linuxmt/sched.h>
#include <linuxmt/fs.h>
#include <linuxmt/mm.h>
#include <linuxmt/string.h>
#include <linuxmt/minix_fs.h>
#include <linuxmt/stat.h>

Expand Down
1 change: 1 addition & 0 deletions elks/fs/msdos/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <linuxmt/errno.h>
#include <linuxmt/stat.h>
#include <linuxmt/mm.h>
#include <linuxmt/string.h>
#include <linuxmt/debug.h>

static size_t msdos_dir_read(struct inode *dir, struct file *filp, char *buf, size_t count)
Expand Down
40 changes: 19 additions & 21 deletions elks/include/linuxmt/mm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,27 @@ struct segment {
list_s free;
seg_t base;
segext_t size;
word_t flags;
word_t ref_count;
byte_t flags;
byte_t ref_count;
word_t pid;
};

typedef struct segment segment_s;

// TODO: convert to tag
#define SEG_FLAG_FREE 0x0000
#define SEG_FLAG_USED 0x0080
#define SEG_FLAG_ALIGN1K 0x0040
#define SEG_FLAG_TYPE 0x000F
#define SEG_FLAG_CSEG 0x0001
#define SEG_FLAG_DSEG 0x0002
#define SEG_FLAG_EXTBUF 0x0003
#define SEG_FLAG_RAMDSK 0x0004
#define SEG_FLAG_FREE 0x00
#define SEG_FLAG_USED 0x80
#define SEG_FLAG_ALIGN1K 0x40
#define SEG_FLAG_TYPE 0x0F
#define SEG_FLAG_CSEG 0x01
#define SEG_FLAG_DSEG 0x02
#define SEG_FLAG_EXTBUF 0x03
#define SEG_FLAG_RAMDSK 0x04
#define SEG_FLAG_PROG 0x05

#ifdef __KERNEL__

#include <linuxmt/sched.h>
#include <linuxmt/errno.h>
#include <linuxmt/kernel.h>
#include <linuxmt/string.h>

#define VERIFY_READ 0
#define VERIFY_WRITE 1

#include <linuxmt/memory.h>

#define verify_area(mode,point,size) verfy_area(point,size)

/*@-namechecks@*/

Expand All @@ -47,6 +39,11 @@ int strlen_fromfs(void *,size_t);

/*@+namechecks@*/

#define VERIFY_READ 0
#define VERIFY_WRITE 1

#define verify_area(mode,point,size) verfy_area(point,size)

int verfy_area(void *,size_t);
void put_user_char(unsigned char,void *);
void put_user(unsigned short,void *);
Expand All @@ -65,9 +62,10 @@ void seg_free (segment_s *);

segment_s * seg_get (segment_s *);
void seg_put (segment_s *);

segment_s * seg_dup (segment_s *);

void seg_free_pid(pid_t pid);

void mm_get_usage (unsigned int * free, unsigned int * used);

#endif // __KERNEL__
Expand Down
4 changes: 3 additions & 1 deletion elks/kernel/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ void do_exit(int status)
seg_put(current->mm.seg_code);
if (current->mm.seg_data)
seg_put(current->mm.seg_data);

current->mm.seg_code = current->mm.seg_data = 0;

/* free program allocated memory */
seg_free_pid(current->pid);

#if BLOAT
/* Keep all of the family stuff straight */
struct task_struct *task;
Expand Down
2 changes: 2 additions & 0 deletions elks/kernel/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <linuxmt/config.h>
#include <arch/segment.h>
#include <linuxmt/mm.h>
#include <linuxmt/kernel.h>
#include <linuxmt/sched.h>
#include <linuxmt/kdev_t.h>
#include <linuxmt/major.h>
#include <linuxmt/ntty.h>
Expand Down
1 change: 1 addition & 0 deletions elks/kernel/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <linuxmt/sched.h>
#include <linuxmt/errno.h>
#include <linuxmt/mm.h>
#include <linuxmt/string.h>

#include <linuxmt/config.h>
#include <arch/system.h>
Expand Down
23 changes: 8 additions & 15 deletions elkscmd/disk_utils/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@

/* FIXME remove all commented out code with RUBOUT in it */


#define ROOT_INO 1

#define UPPER(size,n) ((size+((n)-1))/(n))
Expand Down Expand Up @@ -130,7 +129,7 @@ static char inode_map[BLOCK_SIZE * MINIX_I_MAP_SLOTS];
static char zone_map[BLOCK_SIZE * MINIX_Z_MAP_SLOTS];

static unsigned char * inode_count = NULL;
static unsigned char * zone_count = NULL;
static unsigned char __far * zone_count = NULL;

void recursive_check(unsigned int ino);

Expand Down Expand Up @@ -543,20 +542,14 @@ void read_tables(void)
if (!inode_buffer)
die("Unable to allocate buffer for inodes"); RUBOUT */

/* check for filesystem too large since libc malloc fails improperly FIXME */
if (ZONES > 32767) {
fprintf(stderr, "Filesystem too large to check (%u zones, max 32767)\n", ZONES);
exit(8);
}

printd("inodes %d zones %d\n", INODES, ZONES);
printd("imaps %d zmaps %d\n", IMAPS, ZMAPS);
inode_count = malloc(INODES);
if (!inode_count)
die("Unable to allocate buffer for inode count");
zone_count = malloc(ZONES);
zone_count = fmemalloc(ZONES); /* ZONES <= 64K */
if (!zone_count)
die("Unable to allocate buffer for zone count");
printd("inodes %d zones %d\n", INODES, ZONES);
printd("imaps %d zmaps %d\n", IMAPS, ZMAPS);
die("Unable to allocate main memory for zone count");
if ((IMAPS * BLOCK_SIZE) != read(IN, inode_map, (IMAPS * BLOCK_SIZE)))
die("Unable to read inode map");
if ((ZMAPS * BLOCK_SIZE) != read(IN, zone_map, (ZMAPS * BLOCK_SIZE)))
Expand All @@ -569,8 +562,8 @@ void read_tables(void)
errors_uncorrected = 1;
}
if (show) {
printf("%d inodes\n",INODES);
printf("%d blocks\n",ZONES);
printf("%u inodes\n",INODES);
printf("%u blocks\n",ZONES);
printf("Pre-zone blocks 2+%d+%d+%d %d\n", IMAPS, ZMAPS, INODE_BLOCKS, NORM_FIRSTZONE);
printf("Firstdatazone=%d (%d)\n",FIRSTZONE,NORM_FIRSTZONE);
printf("Zonesize=%d\n",BLOCK_SIZE<<ZONESIZE);
Expand Down Expand Up @@ -889,7 +882,7 @@ void check(void)
{
printd("check\n");
memset(inode_count,0,INODES*sizeof(*inode_count));
memset(zone_count,0,ZONES*sizeof(*zone_count));
fmemset(zone_count,0,ZONES*sizeof(*zone_count));
check_zones(ROOT_INO);
if (verbose > 1) {
printf("/");
Expand Down
2 changes: 1 addition & 1 deletion elkscmd/rootfs_template/bootopts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
#init=/bin/init 3 n # multiuser serial no /etc/rc.sys
#init=/bin/sh # singleuser shell
#root=hda1 ro # root hd partition 1, read-only
#console=ttyS0,19200 3 # serial console
console=ttyS0,19200 3 # serial console
2 changes: 1 addition & 1 deletion elkscmd/sys_utils/meminfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void dump_heap(int fd)
word_t total_free = 0;
long total_segsize = 0;
static char *heaptype[] = { "free", "SEG ", "STR ", "TTY ", "INT ", "BUFH", "PIPE" };
static char *segtype[] = { "free", "CSEG", "DSEG", "BUF ", "RDSK" };
static char *segtype[] = { "free", "CSEG", "DSEG", "BUF ", "RDSK", "PROG" };

printf(" HEAP TYPE SIZE SEG TYPE SIZE CNT\n");

Expand Down
3 changes: 3 additions & 0 deletions libc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ extern void *(*__alloca_alloc) __P((size_t));
#define malloc(x) ((*__alloca_alloc)(x))
#endif

/* alloc from main memory */
void __far *fmemalloc(unsigned long size);

#endif
3 changes: 2 additions & 1 deletion libc/include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ extern void * memccpy __P ((void*, void*, int, size_t));
extern void * memchr __P ((__const void*, __const int, size_t));
extern void * memset __P ((void*, int, size_t));
extern int memcmp __P ((__const void*, __const void*, size_t));

extern void * memmove __P ((void*, void*, size_t));

void __far *fmemset(void __far *buf, int c, size_t l);

/* Error messages */
extern char * strerror __P ((int));

Expand Down
1 change: 1 addition & 0 deletions libc/malloc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ OBJS = \
noise.o \
realloc.o \
sbrk.o \
fmemalloc.o \

.PHONY: all

Expand Down
Loading

0 comments on commit 975c6d8

Please sign in to comment.