Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys_process_exec crashes on qemu-2.11.1 #191

Open
jmazanec15 opened this issue Nov 4, 2018 · 14 comments
Open

sys_process_exec crashes on qemu-2.11.1 #191

jmazanec15 opened this issue Nov 4, 2018 · 14 comments

Comments

@jmazanec15
Copy link
Collaborator

jmazanec15 commented Nov 4, 2018

I am trying to create a window manager user program in which a list of programs are passed to the window manager and the window manager forks and execs each of them in their own windows. However, there seems to be a bug in the exec call.

Initially, running this fork and exec program fails:

#include "library/syscalls.h"
#include "library/string.h"

int main()
{
	int pid = process_fork();

	if (pid == 0) {
		printf("hello world, I am the child %d.\n", process_self());
		const char *args[] = { "snake.exe" };
		process_exec("snake.exe", args, 1);
	} else {
		printf("hello world, I am the parent %d.\n", process_self());
		struct process_info info;
		process_wait(&info, -1);
		process_reap(info.pid);
	}

	return 0;
}

It produces the following exception:
image

After doing some debugging, I have found that something goes wrong in the process_switch function. At the end of the sys_process_exec, it calls process_yield, which calls process_switch(PROCESS_STATE_READY). Here is the code for process_switch:

static void process_switch(int newstate)
{
	interrupt_block();

	if(current) {
		if(current->state != PROCESS_STATE_CRADLE) {
			asm("pushl %ebp");
			asm("pushl %edi");
			asm("pushl %esi");
			asm("pushl %edx");
			asm("pushl %ecx");
			asm("pushl %ebx");
			asm("pushl %eax");
		      asm("movl %%esp, %0":"=r"(current->kstack_ptr));
		}

		interrupt_stack_pointer = (void *) INTERRUPT_STACK_TOP;
		current->state = newstate;

		if(newstate == PROCESS_STATE_READY) {
			list_push_tail(&ready_list, &current->node);
		}
		if(newstate == PROCESS_STATE_GRAVE) {
			list_push_tail(&grave_list, &current->node);
		}
	}

	current = 0;

	while(1) {
		current = (struct process *) list_pop_head(&ready_list);
		if(current)
			break;

		interrupt_unblock();
		interrupt_wait();
		interrupt_block();
	}
	current->state = PROCESS_STATE_RUNNING;
	interrupt_stack_pointer = current->kstack_top;

	asm("movl %0, %%cr3"::"r"(current->pagetable));
	asm("movl %0, %%esp"::"r"(current->kstack_ptr));

	asm("popl %eax");
	asm("popl %ebx");
	asm("popl %ecx");
	asm("popl %edx");
	asm("popl %esi");
	asm("popl %edi");
	asm("popl %ebp");

	interrupt_unblock();
}

I have found that the program works by adding a dumby for loop before the while loop as a delay:

	for (int i = 0; i < 100000; ++i)
	{
		continue;
	}

	while(1) {

While this obviously is not a solution, I think it might indicate that there is some kind of race condition going on.

@jmazanec15
Copy link
Collaborator Author

Just adding something as simple as int t = 0; instead of adding the for loop gets rid of the exception as well.

@ethanmw
Copy link
Collaborator

ethanmw commented Nov 4, 2018

Sounds like it's allocating the integer in both cases that fixes it, so maybe it's a stack thing, not a time thing?

@jmazanec15
Copy link
Collaborator Author

Yes I think you are right.

@dthain
Copy link
Owner

dthain commented Nov 5, 2018

Hmm, I am not able to reproduce the problem on using the argv-repair branch:

make
qemu-system-i386 -cdrom basekernel.iso
mount 2 cdrom
cd bin
run exctst.exe

@dthain
Copy link
Owner

dthain commented Nov 5, 2018

snapshot2

@dthain
Copy link
Owner

dthain commented Nov 5, 2018

@jmazanec15 Can you double check whether you have the same problem with a fresh checkout of the argv-repair branch, and also the master branch?

@jmazanec15
Copy link
Collaborator Author

I ran the following commands on the terminal in a new repository:
git clone [email protected]:dthain/basekernel.git
git checkout origin/argv-repair

I then changed the Makefile.config to look like this so it would work on my Mac:

KERNEL_CCFLAGS=-Wall -c -ffreestanding -fno-pie -g
ISOGEN=mkisofs

# These settings select the native compiler,
# which is likely to work on native linux-x86.
#
CC=gcc -m32
LD=ld -melf_i386
AR=ar
OBJCOPY=objcopy

# If you are compiling from another platform,
# then use the script build-cross-compiler.sh
# add cross/bin to your path, and uncomment these lines:
CC=i686-elf-gcc
LD=i686-elf-ld
AR=i686-elf-ar
OBJCOPY=i686-elf-objcopy

And then ran in the terminal:
make
qemu-system-i386 -cdrom basekernel.iso

And then ran in base kernel:
mount 2 cdrom
chdir bin
run exctst.exe

And got the same error:
image

(I also removed the \f in the interrupt.c file so that the exception would not flush the entire screen.)

@dthain
Copy link
Owner

dthain commented Nov 5, 2018

Urk, sounds like we have a subtle different between mac and linux.
I'm hard-pressed to understand why a debug interrupt is raised.

Some more ideas to try:

  • Try running it with gdb attached, see if you can determine what is the instruction at eip 0x06fe0000
  • Try changing the interrupt handler to simply return in the case of a debugging trap.

@dthain
Copy link
Owner

dthain commented Nov 5, 2018

Er wait -- do you get the same problem when running from the master branch?

@jmazanec15
Copy link
Collaborator Author

I got the same problem running from the master branch. I also just ran it from Ubuntu and got the same problem. I will try using gdb and determine the instruction. I know Ethan setup GDB so I'll see if he can help me set that up.

@dthain
Copy link
Owner

dthain commented Nov 5, 2018

FWIW, I'm using qemu-2.0.0, what do you have?

@jmazanec15
Copy link
Collaborator Author

jmazanec15 commented Nov 5, 2018

It looks I am using 2.11.1 for Ubuntu and 3.0.0 for my Mac.

This was referenced Nov 6, 2018
@dthain dthain changed the title Problem in process_switch affecting sys_process_exec sys_process_exec crashes on qemu-2.11.1 Dec 12, 2018
@dthain
Copy link
Owner

dthain commented Feb 6, 2019

Is this one actually fixed?
I believe I observed it a few weeks back on my mac, but didn't have time to work on it then...

@dthain dthain reopened this Feb 6, 2019
@jmazanec15
Copy link
Collaborator Author

My mistake. I was interpreting the output of exectest wrong. It is broken for qemu 3.0.0 as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants