Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
my-kernel
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ASE-2021
my-kernel
Commits
9ae56c41
Commit
9ae56c41
authored
4 years ago
by
Thierno souleymane Bah
Browse files
Options
Downloads
Patches
Plain Diff
feat(contexts manager lib added, costumized malloc and free added)
parent
547ddeae
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/context.h
+47
-0
47 additions, 0 deletions
include/context.h
src/context.c
+126
-0
126 additions, 0 deletions
src/context.c
with
173 additions
and
0 deletions
include/context.h
0 → 100644
+
47
−
0
View file @
9ae56c41
#if !defined(CONTEXT_H)
#define CONTEXT_H
#define STACK_SIZE 16384
#define CTX_MAX 16
typedef
int
(
func_t
)(
int
);
typedef
void
(
funct_t
)(
void
*
);
typedef
enum
{
CTX_INIT
,
CTX_EXEC
,
CTX_END
}
ctx_status_t
;
typedef
struct
ctx_s
{
void
*
esp
;
void
*
ebp
;
char
stack
[
STACK_SIZE
];
funct_t
*
f
;
void
*
args
;
ctx_status_t
status
;
struct
ctx_s
*
next_ctx
;
char
used
;
}
ctx_t
;
static
ctx_t
ctx_heap
[
CTX_MAX
];
ctx_t
*
malloc_ctx
();
void
free_ctx
(
ctx_t
*
ctx
);
int
init_ctx
(
ctx_t
*
,
funct_t
,
void
*
);
void
switch_to_ctx
(
ctx_t
*
);
void
start_ctx
();
ctx_t
*
create_ctx
(
funct_t
,
void
*
);
void
yield
();
void
start_sched
();
#endif // CONTEXT_H
This diff is collapsed.
Click to expand it.
src/context.c
0 → 100644
+
126
−
0
View file @
9ae56c41
#include
"context.h"
#include
"utils.h"
static
ctx_t
*
current_ctx
=
NULL
;
static
ctx_t
*
ring_ctx
=
NULL
;
ctx_t
*
malloc_ctx
()
{
int
i
;
for
(
i
=
0
;
i
<
CTX_MAX
&&
ctx_heap
[
i
].
used
;
i
++
)
;
if
(
i
==
CTX_MAX
||
ctx_heap
[
i
].
used
)
return
NULL
;
ctx_heap
[
i
].
used
=
1
;
return
&
ctx_heap
[
i
];
}
void
free_ctx
(
ctx_t
*
ctx
)
{
int
i
;
for
(
i
=
0
;
i
<
CTX_MAX
&&
(
ctx_heap
+
i
)
==
ctx
;
i
++
)
;
if
(
i
==
CTX_MAX
||
!
ctx_heap
[
i
].
used
)
return
;
ctx_heap
[
i
].
used
=
0
;
}
int
init_ctx
(
ctx_t
*
pctx
,
funct_t
f
,
void
*
args
)
{
//pctx->stack = malloc(stack_size);
pctx
->
esp
=
pctx
->
stack
+
STACK_SIZE
-
sizeof
(
void
*
);
pctx
->
ebp
=
pctx
->
stack
+
STACK_SIZE
-
sizeof
(
void
*
);
pctx
->
f
=
f
;
pctx
->
args
=
args
;
pctx
->
status
=
CTX_INIT
;
return
0
;
}
void
switch_to_ctx
(
ctx_t
*
pctx
)
{
if
(
pctx
->
status
==
CTX_END
)
{
// Si c'est l'unique ctx
if
(
pctx
==
pctx
->
next_ctx
)
ring_ctx
=
NULL
;
else
current_ctx
->
next_ctx
=
pctx
->
next_ctx
;
free_ctx
(
pctx
);
yield
();
return
;
}
assert
(
pctx
->
status
!=
CTX_END
);
if
(
current_ctx
!=
NULL
)
{
// Sauvegarder où on en est avec le ctx courant
asm
(
"mov %%esp, %0"
"
\n\t
"
"mov %%ebp, %1"
:
"=r"
(
current_ctx
->
esp
),
"=r"
(
current_ctx
->
ebp
));
}
current_ctx
=
pctx
;
asm
(
"mov %0, %%esp"
"
\n\t
"
"mov %1, %%ebp"
:
:
"r"
(
current_ctx
->
esp
),
"r"
(
current_ctx
->
ebp
));
if
(
current_ctx
->
status
==
CTX_INIT
)
start_ctx
();
}
void
start_ctx
()
{
current_ctx
->
status
=
CTX_EXEC
;
irq_enable
();
current_ctx
->
f
(
current_ctx
->
args
);
current_ctx
->
status
=
CTX_END
;
puts
(
"
\n
function termine
\n
"
);
yield
();
}
ctx_t
*
create_ctx
(
funct_t
f
,
void
*
args
)
{
ctx_t
*
new_ctx
=
malloc_ctx
();
if
(
ring_ctx
==
NULL
)
{
new_ctx
->
next_ctx
=
new_ctx
;
ring_ctx
=
new_ctx
;
}
else
{
new_ctx
->
next_ctx
=
ring_ctx
->
next_ctx
;
ring_ctx
->
next_ctx
=
new_ctx
;
}
init_ctx
(
new_ctx
,
f
,
args
);
return
new_ctx
;
}
void
yield
()
{
irq_disable
();
if
(
ring_ctx
==
NULL
)
{
puts
(
"
\n
System stopped
\n
"
);
for
(;;)
;
}
if
(
current_ctx
==
NULL
)
switch_to_ctx
(
ring_ctx
->
next_ctx
);
else
switch_to_ctx
(
current_ctx
->
next_ctx
);
irq_enable
();
}
void
start_sched
()
{}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment