Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
rcore-os
RCore Tutorial V3
提交
2166fecf
R
RCore Tutorial V3
项目概览
rcore-os
/
RCore Tutorial V3
上一次同步 1 年多
通知
15
Star
793
Fork
260
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
RCore Tutorial V3
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
2166fecf
编写于
5月 01, 2022
作者:
chyyuu1972
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add kernel thread, but run error
上级
ca433ad3
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
206 addition
and
3 deletion
+206
-3
os/src/main.rs
os/src/main.rs
+1
-0
os/src/mm/frame_allocator.rs
os/src/mm/frame_allocator.rs
+1
-0
os/src/mm/memory_set.rs
os/src/mm/memory_set.rs
+9
-0
os/src/task/context.rs
os/src/task/context.rs
+12
-3
os/src/task/id.rs
os/src/task/id.rs
+38
-0
os/src/task/mod.rs
os/src/task/mod.rs
+79
-0
os/src/task/process.rs
os/src/task/process.rs
+26
-0
os/src/task/task.rs
os/src/task/task.rs
+40
-0
未找到文件。
os/src/main.rs
浏览文件 @
2166fecf
...
...
@@ -54,6 +54,7 @@ pub fn rust_main() -> ! {
trap
::
enable_timer_interrupt
();
timer
::
set_next_trigger
();
task
::
stackless_coroutine
::
kernel_stackless_coroutine_test
();
task
::
kernel_stackful_coroutine_test
();
fs
::
list_apps
();
task
::
add_initproc
();
task
::
run_tasks
();
...
...
os/src/mm/frame_allocator.rs
浏览文件 @
2166fecf
...
...
@@ -5,6 +5,7 @@ use alloc::vec::Vec;
use
core
::
fmt
::{
self
,
Debug
,
Formatter
};
use
lazy_static
::
*
;
#[derive(Clone)]
pub
struct
FrameTracker
{
pub
ppn
:
PhysPageNum
,
}
...
...
os/src/mm/memory_set.rs
浏览文件 @
2166fecf
...
...
@@ -242,8 +242,17 @@ impl MemorySet {
//*self = Self::new_bare();
self
.areas
.clear
();
}
pub
fn
kernel_copy
()
->
Self
{
let
areas
=
KERNEL_SPACE
.exclusive_access
()
.areas
.clone
();
Self
{
page_table
:
PageTable
::
from_token
(
kernel_token
()),
areas
:
areas
,
}
}
}
#[derive(Clone)]
pub
struct
MapArea
{
vpn_range
:
VPNRange
,
data_frames
:
BTreeMap
<
VirtPageNum
,
FrameTracker
>
,
...
...
os/src/task/context.rs
浏览文件 @
2166fecf
...
...
@@ -2,9 +2,9 @@ use crate::trap::trap_return;
#[repr(C)]
pub
struct
TaskContext
{
ra
:
usize
,
sp
:
usize
,
s
:
[
usize
;
12
],
pub
ra
:
usize
,
pub
sp
:
usize
,
pub
s
:
[
usize
;
12
],
}
impl
TaskContext
{
...
...
@@ -15,6 +15,15 @@ impl TaskContext {
s
:
[
0
;
12
],
}
}
pub
fn
kthread_init
()
->
Self
{
Self
{
ra
:
0xAAAAAAAA
,
sp
:
0xBBBBBBBB
,
s
:
[
0xCCCCCCCC
;
12
],
}
}
pub
fn
goto_trap_return
(
kstack_ptr
:
usize
)
->
Self
{
Self
{
ra
:
trap_return
as
usize
,
...
...
os/src/task/id.rs
浏览文件 @
2166fecf
...
...
@@ -221,3 +221,41 @@ impl Drop for TaskUserRes {
self
.dealloc_user_res
();
}
}
use
alloc
::
alloc
::{
alloc
,
dealloc
,
Layout
};
#[derive(Clone)]
pub
struct
KStack
(
usize
);
const
STACK_SIZE
:
usize
=
0x8000
;
impl
KStack
{
pub
fn
new
()
->
KStack
{
let
bottom
=
unsafe
{
alloc
(
Layout
::
from_size_align
(
STACK_SIZE
,
STACK_SIZE
)
.unwrap
())
}
as
usize
;
KStack
(
bottom
)
}
pub
fn
top
(
&
self
)
->
usize
{
self
.0
+
STACK_SIZE
}
}
use
core
::
fmt
::{
self
,
Debug
,
Formatter
};
impl
Debug
for
KStack
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
<
'_
>
)
->
fmt
::
Result
{
f
.write_fmt
(
format_args!
(
"KStack:{:#x}"
,
self
.0
))
}
}
impl
Drop
for
KStack
{
fn
drop
(
&
mut
self
)
{
unsafe
{
dealloc
(
self
.0
as
_
,
Layout
::
from_size_align
(
STACK_SIZE
,
STACK_SIZE
)
.unwrap
()
);
}
}
}
\ No newline at end of file
os/src/task/mod.rs
浏览文件 @
2166fecf
...
...
@@ -131,3 +131,82 @@ pub fn current_add_signal(signal: SignalFlags) {
let
mut
process_inner
=
process
.inner_exclusive_access
();
process_inner
.signals
|
=
signal
;
}
#[no_mangle]
pub
fn
kthread_create
(
f
:
fn
())
{
println!
(
"kthread_create"
);
//�����ں��߳�
let
new_tcb
=
TaskControlBlock
::
create_kthread
(
f
);
// let kernel_stack = new_tcb.get_kernel_stack();
let
new_task
=
Arc
::
new
(
new_tcb
);
//��������������,���û��̷߳���һ�����.
// println!("add task");
add_task
(
Arc
::
clone
(
&
new_task
));
}
#[no_mangle]
pub
fn
kernel_stackful_coroutine_test
()
{
println!
(
"kernel_stackful_coroutine_test"
);
kthread_create
(
||
{
let
id
=
1
;
println!
(
"kernel thread {:?} STARTING"
,
id
);
for
i
in
0
..
10
{
println!
(
"kernel thread: {} counter: {}"
,
id
,
i
);
}
println!
(
"kernel thread {:?} FINISHED"
,
id
);
kthread_stop
();
}
);
kthread_create
(
||
{
let
id
=
2
;
println!
(
"kernel thread {:?} STARTING"
,
2
);
for
i
in
0
..
10
{
println!
(
"kernel thread: {} counter: {}"
,
2
,
i
);
kthread_yield
();
}
println!
(
"kernel thread {:?} FINISHED"
,
2
);
kthread_stop
();
}
);
kthread_create
(
||
{
let
id
=
3
;
println!
(
"kernel thread {:?} STARTING"
,
3
);
for
i
in
0
..
10
{
println!
(
"kernel thread: {} counter: {}"
,
3
,
i
);
kthread_yield
();
}
println!
(
"kernel thread {:?} FINISHED"
,
3
);
kthread_stop
();
}
);
}
pub
fn
kthread_stop
(){
do_exit
();
}
#[no_mangle]
pub
fn
do_exit
(){
println!
(
"kthread do exit"
);
exit_kthread_and_run_next
(
0
);
panic!
(
"Unreachable in sys_exit!"
);
}
pub
fn
kthread_yield
(){
suspend_current_and_run_next
();
}
#[no_mangle]
pub
fn
exit_kthread_and_run_next
(
exit_code
:
i32
)
{
println!
(
"exit_kthread_and_run_next"
);
// we do not have to save task context
let
mut
_u
nused
=
TaskContext
::
zero_init
();
schedule
(
&
mut
_u
nused
as
*
mut
_
);
}
\ No newline at end of file
os/src/task/process.rs
浏览文件 @
2166fecf
...
...
@@ -256,4 +256,30 @@ impl ProcessControlBlock {
pub
fn
getpid
(
&
self
)
->
usize
{
self
.pid
.0
}
pub
fn
kernel_process
()
->
Arc
<
Self
>
{
let
memory_set
=
MemorySet
::
kernel_copy
();
let
process
=
Arc
::
new
(
ProcessControlBlock
{
pid
:
super
::
pid_alloc
(),
inner
:
unsafe
{
UPSafeCell
::
new
(
ProcessControlBlockInner
{
is_zombie
:
false
,
memory_set
:
memory_set
,
parent
:
None
,
children
:
Vec
::
new
(),
exit_code
:
0
,
fd_table
:
Vec
::
new
(),
signals
:
SignalFlags
::
empty
(),
tasks
:
Vec
::
new
(),
task_res_allocator
:
RecycleAllocator
::
new
(),
mutex_list
:
Vec
::
new
(),
semaphore_list
:
Vec
::
new
(),
condvar_list
:
Vec
::
new
(),
})
},
});
process
}
}
os/src/task/task.rs
浏览文件 @
2166fecf
...
...
@@ -23,6 +23,11 @@ impl TaskControlBlock {
let
inner
=
process
.inner_exclusive_access
();
inner
.memory_set
.token
()
}
// pub fn get_kernel_stack(&self) -> usize {
// self.kstack
// }
}
pub
struct
TaskControlBlockInner
{
...
...
@@ -68,6 +73,41 @@ impl TaskControlBlock {
},
}
}
pub
fn
create_kthread
(
f
:
fn
())
->
Self
{
use
crate
::
mm
::{
KERNEL_SPACE
,
PhysPageNum
,
VirtAddr
,
PhysAddr
};
let
process
=
ProcessControlBlock
::
kernel_process
();
let
process
=
Arc
::
downgrade
(
&
process
);
let
kstack
=
kstack_alloc
();
let
kernelstack
=
crate
::
task
::
id
::
KStack
::
new
();
let
kstack_top
=
kernelstack
.top
();
let
mut
context
=
TaskContext
::
kthread_init
();
let
context_addr
=
&
context
as
*
const
TaskContext
as
usize
;
let
pa
=
PhysAddr
::
from
(
context_addr
);
let
context_ppn
=
pa
.floor
();
context
.ra
=
f
as
usize
;
context
.sp
=
kstack_top
;
println!
(
"context ppn :{:#x?}"
,
context_ppn
);
Self
{
process
,
kstack
,
inner
:
unsafe
{
UPSafeCell
::
new
(
TaskControlBlockInner
{
res
:
None
,
trap_cx_ppn
:
context_ppn
,
task_cx
:
context
,
task_status
:
TaskStatus
::
Ready
,
exit_code
:
None
,
})
},
}
}
}
#[derive(Copy,
Clone,
PartialEq)]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录